Sunday, August 24, 2014

Stack - Data Structure C++

// Stack.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class Node{
public:
int data;
Node *next;
};

class Stack {
public:
Node *top;
Node* current;
Node *bottom;
Stack(){
top=NULL;
current=NULL;
bottom=NULL;
}
void gettop(){
cout<<top->data;
}
void pop(){
if(top==NULL)
{
cout<<"Stack is empty: \n";
}
else{
Node *temp;
temp =top;
top=top->next;
delete(temp);
}

}
void push(){
int n;
cout<<"\nEnter the number you want to push : ";
cin >>n;
if(top==NULL){
Node *temp = new Node;
temp->data=n;
temp->next=NULL;
top=temp;
bottom=temp;
delete(temp);
}
else{
Node *temp = new Node;
temp->data=n;
temp->next=top;
top=temp;
}
}

void display(){
current=top;
while(current!=bottom){
cout<<current->data;
cout<<endl;
current=current->next;
}
cout<<current->data;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
Stack s;
int option;

cout<<"Enter stack operation\n1: Push()\n2: Pop()\n3: Top\n4: Display Stack\nPress 'e' to exit\n ";
cin >> option;

while(option!='e'){
switch(option){
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.gettop();
break;
case 4:
s.display();
break;
}
cout<<"\nEnter stack operation\n1: Push()\n2: Pop()\n3: Top\n4: Display Stack\nPress 'e' to exit\n ";
cin >> option;
}
return 0;
}

Que - Data Structure C++

// Que.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class Node{
public:
int data;
Node *next;

Node(){
data=0;
next=NULL;
}
};

class Que{
public:
Node *first;
Node *last;
Node *current;

Que(){
first = NULL;
last=NULL;
current=NULL;
}

void enque(int n){
if(last==NULL){
Node *temp=new Node;
temp->data=n;
temp->next=NULL;
first=temp;
last=temp;
delete(temp);
}
else{
Node *temp= new Node;;
temp->data=n;
temp->next=NULL;
last->next=temp;
last=temp;
delete(temp);
}
}
void deque(){
if(last==NULL){
cout<<"\nThe Que is empty\n ";
}
else{
current=first;
first=first->next;
cout<<"Data dequeued is : "<<current->data<<endl;
delete(current);
}
}
void display(){
current=first;
while(current!=last){
cout<<current->data;
current=current->next;
}
cout<<current->data;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
Que q;
int option,n;

cout<<"Please select your que operation:\n1: Enque()\n2:Deque\n3: Display\n Enter 'e' to exit\n";
cin>>option;
while(option!='e'){
switch(option){
case 1:
cout<<"\nEnter the data you want to enque: ";
cin>>n;
q.enque(n);
break;
case 2:
q.deque();
break;
case 3:
q.display();
break;
}
cout<<"\nPlease select your que operation:\n1: Enque()\n2:Deque\n3: Display\n Enter 'e' to exit\n";
cin>>option;
}
return 0;
}

Priority Que - Data Structure C++

// P_Que.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class Node{
public:
int data;
int prio;
Node *next;
Node *previous;

Node(){
data=0;
prio=0;
next=NULL;
previous=NULL;
}
};

class P_Que{
public:
Node *first;
Node* last;
Node *current;

P_Que(){
first=NULL;
last=NULL;
current=NULL;
}

void enque(int n,int p){
if(first==NULL){
Node *temp= new Node;
temp->data=n;
temp->prio=p;
temp->next=NULL;
temp->previous=NULL;
first=temp;
last=temp;
}
else{
current=first;
Node *temp =new Node;
temp->data=n;
temp->prio=p;
while(current->next!=NULL){
if(temp->prio>current->prio){
temp->next=current;
temp->previous=current->previous;
current=current->next;
}
}
}
}

void display(){
current=first;
while(current->next!=NULL){
cout<<current->data<<endl;
current=current->next;
}

}
};

int _tmain(int argc, _TCHAR* argv[])
{
P_Que q;
int option,n,p;

cout<<"\nSelect an option:\n1: Enque()\n2:Display Que\nEnter 'e' to exit.\n";
cin>> option;

while(option!='e'){
switch(option){
case 1:
cout<<"\nEnter the data you want to enque: ";
cin>>n;
cout<<"\nEnter it's priority: ";
cin>> p;
q.enque(n,p);
break;
case 2:
q.display();
break;
}
cout<<"\nSelect an option:\n1: Enque()\n2:Display Que\nEnter 'e' to exit.\n";
cin>> option;
}
return 0;
}

Linked List- Data Structure C++

// Linked_List.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

class Node{
public:
int data;
Node *next;
Node *previous;

Node(){
next=NULL;
previous=NULL;
}
};

class LinkedList{
private:
Node *head;
Node *current;
Node *end;
public:
LinkedList()
{
head=NULL;
current=NULL;
end=NULL;
}
void addElement()
{
Node *temp;
temp=new Node;
cout<<"\nEnter element data: ";
cin>> temp->data;
if(head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
current=head;
while(current->next != NULL)
{
current=current->next;
}
current->next =temp;
}
}

void remove()
{
int n;
cout<<"Enter the element you want to remove: ";
cin>>n;
current=head;
Node *previous=current;
while(current->data!=n)
{
previous=current;
current =current->next;
}
Node *temp=current;
previous->next=current->next;
delete temp;
}

void countElements()
{
int count=0;
current =head;
while(current->next!=NULL)
{
current=current->next;
count++;
}
count++;
cout<<"The total number of elements are: "<<count;
}

void display(){
current=head;
cout<<"The list contains the following elements:\n";
while(current->next!=NULL)
{
cout<<current->data<<endl;
current=current->next;
}
cout<<current->data;
}
};


int _tmain(int argc, _TCHAR* argv[])
{
LinkedList a;
int option;

cout<< "Enter you choice:\n1:Add Element\n2:Remove Element\n3:Total number of elements\n4:Display List\nEnter 'e' to end\n";
cin>>option;

while(option!='e'){

switch (option){
case 1:
a.addElement();
break;
case 2:
a.remove();
break;
case 3:
a.countElements();
break;
case 4:
a.display();
break;
}
cout<< "Enter you choice:\n1:Add Element\n2:Remove Element\n3:Total number of elements\n4:Display List\nEnter 'e' to end\n";
cin>>option;
}
return 0;
}

Bank Account Project C++

// Bank_Accout.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class BankAccount{
private:
int x;
int balance;
public:
void withdraw(){
cout<<"Enter the amount you want to withdraw: ";
cin >> x;
balance-=x;
cout<< "Your remaining balance is: "<<balance;
}

void deposit(){
cout<<"Enter the amount you want to deposit: ";
cin >> x;
balance+=x;
cout<< "Your new balance is: "<<balance;
}

void showBalance(){
cout<< "Your current balance is: "<<balance;
}

BankAccount(){
cout<<"Enter the amount in the account: ";
cin>>balance;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
BankAccount a;
int n;

cout<< "Enter your choice: \n1: Withdraw amount\n2: Desposit amount\n3: Check Balance";
cin >> n;

while(n!='e'){

cout<< "Enter your choice: \n1: Withdraw amount\n2: Desposit amount\nCheck Balance";
cin >> n;

switch (n){
case 1:
a.withdraw();
break;
case 2:
a.deposit();
break;
case 3:
a.showBalance();
break;
default:
cout<< "Enter a valid option\n";

}

}
_getch();
return 0;
}

Tic Tac Toe Game C++

// TicTacToe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

char map[9] = {'0','1','2','3','4','5','6','7','8',};

void sortp1(int p[]);
void sortp2(int p[]);

void display(int, int);
void display(int, int);

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int p1[5];
int p2[5];
int map[9];
int t=0;


cout<<  "0" << " | " << "1" << " | " << "2" <<endl;
cout<< "========="<<endl;
cout<<  "3" << " | " << "4" << " | " << "5" <<endl;
cout<< "========="<<endl;
cout<<  "6" << " | " << "7" << " | " << "8" <<endl;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1, p1[t]);

cout<< "\nTurn: Player 2=>";
cin>> p2[t];
display(2,p2[t]);

t++;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1,p1[t]);

cout<< "\nTurn: Player 2=>";
cin>> p2[t];
display(2, p2[t]);

t++;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1, p1[t]);

cout<< "\nTurn: Player 2=>";
cin>> p2[t];
display(2,p2[t]);

t++;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1, p1[t]);

cout<< "\nTurn: Player 2=>";
cin>> p2[t];
display(2,p2[t]);

t++;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1, p1[t]);

//for(int j=0; j<5; j++)
//{
// cout<<p1[j] << "\n";
//}
//for(int j=0; j<4; j++)
//{
// cout<<p2[j] << "\n";
//}

sortp1(p1);
sortp2(p2);

if( p1[0] == 0 && p1[1] == 1 && p1[2] == 2 || p1[0] == 3 && p1[1] == 4 && p1[2] == 5 || p1[0] ==6 && p1[1] == 7 && p1[2] ==8
||p1[0] ==0 && p1[1] ==3 && p1[2] ==6 || p1[0] ==1 && p1[1] ==4 && p1[2] ==7 || p1[0] ==2 && p1[3] ==5 && p1[6] ==8
|| p1[0] == 0 && p1[1] ==4 && p1[2] == 8 || p1[0] == 2 && p1[1] == 4 && p1[2] == 6)
{
cout << "Congratulations Player 1 , You Win!";
}
else

if( p2[0] == 0 && p2[1] == 1 && p2[2] == 2 || p2[0] == 3 && p2[1] ==4 && p2[2] == 5 || p2[0] == 6 && p2[1] == 7 && p2[2] == 8
||p2[0] ==0 && p2[1] ==3 && p2[2] ==6 || p2[0] ==1 && p2[1] ==4 && p2[2] ==7 || p2[0] ==2 && p2[3] ==5 && p2[6] ==8
|| p2[0] == 0 && p2[1] ==4 && p2[2] == 8 || p2[0] == 2 && p2[1] ==4 && p2[2] == 6)
{
cout << "Congratulations Player 2 , You Win!";
}
else
cout << "It's a draw";

getch();
return 0;
}

void sortp1(int p[])
{
for(int i=0; i<3;i--)
{
for(int i=0; i<4;i++)
{
int temp;
if(p[i] >p[i+1])
{
temp = p[i];
p[i]=p[i+1];
p[i+1]=temp;
}
}
}

/*for(int j=0; j<5; j++)
{
cout<<p[j] << "\n";
}*/
}

void sortp2(int p[])
{
int temp;
for(int i=0; i<2;i--)
{
for(int i=0; i<3;i++)
{

if(p[i] >p[i+1])
{
temp = p[i];
p[i]=p[i+1];
p[i+1]=temp;
}
}
}
/*for(int j=0; j<4; j++)
{
cout<<p[j] << "\n";
}*/
}

void display(int a, int p)
{




if(a ==1)
{
map[p]='X';
}
if(a == 2)
{
map[p] = 'O';
}

cout<< map[0] << " | " << map[1] << " | " << map[2] <<endl;
cout<< "========="<<endl;
cout<<  map[3] << " | " <<map[4] << " | " << map[5] <<endl;
cout<< "========="<<endl;
cout<<  map[6] << " | " <<map[7] << " | " << map[8] <<endl;

}

AVL Tree

#include "avltree.h"
#include <stdlib.h>
#include "fatal.h"

struct AvlNode
{
ElementType Element;
AvlTree  Left;
AvlTree  Right;
int      Height;
};

AvlTree MakeEmpty( AvlTree T )
{
if( T != NULL )
{
MakeEmpty( T->Left );
MakeEmpty( T->Right );
free( T );
}
return NULL;
}

Position Find( ElementType X, AvlTree T )
{
if( T == NULL )
return NULL;
if( X < T->Element )
return Find( X, T->Left );
else
if( X > T->Element )
return Find( X, T->Right );
else
return T;
}

Position FindMin( AvlTree T )
{
if( T == NULL )
return NULL;
else
if( T->Left == NULL )
return T;
else
return FindMin( T->Left );
}

Position FindMax( AvlTree T )
{
if( T != NULL )
while( T->Right != NULL )
T = T->Right;

return T;
}


static int Height( Position P )
{
if( P == NULL )
return -1;
else
return P->Height;
}


static int Max( int Lhs, int Rhs )
{
return Lhs > Rhs ? Lhs : Rhs;
}


/* This function can be called only if K2 has a left child */
/* Perform a rotate between a node (K2) and its left child */
/* Update heights, then return new root */

static Position SingleRotateWithLeft( Position K2 )
{
Position K1;

K1 = K2->Left;
K2->Left = K1->Right;
K1->Right = K2;

K2->Height = Max( Height( K2->Left ), Height( K2->Right ) ) + 1;
K1->Height = Max( Height( K1->Left ), K2->Height ) + 1;

return K1;  /* New root */
}


/* This function can be called only if K1 has a right child */
/* Perform a rotate between a node (K1) and its right child */
/* Update heights, then return new root */

static Position SingleRotateWithRight( Position K1 )
{
Position K2;

K2 = K1->Right;
K1->Right = K2->Left;
K2->Left = K1;

K1->Height = Max( Height( K1->Left ), Height( K1->Right ) ) + 1;
K2->Height = Max( Height( K2->Right ), K1->Height ) + 1;

return K2;  /* New root */
}


/* This function can be called only if K3 has a left */
/* child and K3's left child has a right child */
/* Do the left-right double rotation */
/* Update heights, then return new root */

static Position DoubleRotateWithLeft( Position K3 )
{
/* Rotate between K1 and K2 */
K3->Left = SingleRotateWithRight( K3->Left );

/* Rotate between K3 and K2 */
return SingleRotateWithLeft( K3 );
}


/* This function can be called only if K1 has a right */
/* child and K1's right child has a left child */
/* Do the right-left double rotation */
/* Update heights, then return new root */

static Position DoubleRotateWithRight( Position K1 )
{
/* Rotate between K3 and K2 */
K1->Right = SingleRotateWithLeft( K1->Right );

/* Rotate between K1 and K2 */
return SingleRotateWithRight( K1 );
}



AvlTree Insert( ElementType X, AvlTree T )
{
if( T == NULL )
{
/* Create and return a one-node tree */
T = malloc( sizeof( struct AvlNode ) );
if( T == NULL )
FatalError( "Out of space!!!" );
else
{
T->Element = X; T->Height = 0;
T->Left = T->Right = NULL;
}
}
else
if( X < T->Element )
{
T->Left = Insert( X, T->Left );
if( Height( T->Left ) - Height( T->Right ) == 2 )
if( X < T->Left->Element )
T = SingleRotateWithLeft( T );
else
T = DoubleRotateWithLeft( T );
}
else
if( X > T->Element )
{
T->Right = Insert( X, T->Right );
if( Height( T->Right ) - Height( T->Left ) == 2 )
if( X > T->Right->Element )
T = SingleRotateWithRight( T );
else
T = DoubleRotateWithRight( T );
}
/* Else X is in the tree already; we'll do nothing */

T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
return T;
}


AvlTree Delete( ElementType X, AvlTree T )
{
printf( "Sorry; Delete is unimplemented; %d remains\n", X );
return T;
}

ElementType Retrieve( Position P )
{
return P->Element;
}

Binary Search Tree

// BST.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

using namespace std;

class Node{
public:
int data;
Node *right;
Node *left;
public:
Node(){
data=0;
left=NULL;
right=NULL;
}

Node(int d){
data=d;
right=NULL;
left=NULL;
cout<<"\nNode created containing the value "<<d<<endl;
}

int getData(){
return data;
}

void setdata(int d){
data=d;
}

Node *getRight(){
if(right==NULL){
return 0;
}
else
return right;
}

void setRight(Node *r){
right=r;
}

Node *getLeft(){
if(left==NULL){
return 0;
}
else
return left;
}

void setLeft(Node *l){
left=l;
}
};

class Tree{
private:
Node *root;
Node *current;
int flag;
public:
Tree(int d){
flag=0;
Node *newNode= new Node();
newNode->data=d;
newNode->right=NULL;
newNode->left=NULL;
root=newNode;
current=root;
}

Node *getCurrent(){
return current;
}

Node *getRoot(){
return root;
}

void insert(Node *current, int d){
if(d==current->data){
cout<<"\nRepeated values are not allowed: ";
}
if(d<current->data){
if(current->left==NULL){
Node *newNode = new Node;
newNode->data = d;
newNode->right=NULL;
newNode->left=NULL;
cout<<"\nThe data is entered in memory at location(newNode) : "<<newNode;
current->left=newNode;
cout<<"\nThe current pointer is pointing at : "<<current<<endl;
}
else{
cout<<"\nGoing left to memory location: "<<current->left<<endl;
insert(current->left,d);
}
}
if(d>current->data){
if(current->right==NULL){
Node *newNode = new Node;
newNode->data = d;
newNode->right=NULL;
newNode->left=NULL;
cout<<"\nThe data is entered in memory at location(newNode) : "<<newNode;
current->right=newNode;
cout<<"\nThe current pointer is pointing at : "<<current<<endl;
}
else {
cout<<"\nGoing right to memory location: "<<current->right<<endl;
insert(current->right,d);
}
}
}

Node *search(Node *current, int d){
if(d==current->data){
return current;
}
else if(d<current->data){
if(current->left==NULL){
return NULL;
}
else search(current->left, d);
}
else if(d>current->data){
if(current->right==NULL){
return NULL;
}
else search(current->right,d);
}
}

bool isLeaf(Node *current){
if(current->left==current->right==NULL)
return true;
else
return false;
}

Node *findMin(Node *current){
if(current->left==NULL){
cout<<"\nReturning the minimum value : "<<current->data;
return current;
}
else findMin(current->left);
}

void deleteNode(Node *current, int d){
if(d<current->data){
 if(current->left!=NULL){
if(d==current->left->data){
if((current->left->left==NULL) && (current->left->right==NULL)){
cout<<"\nDeleting Node containing "<<current->left->data<<endl;
delete (current->left);
current->left=NULL;
}
else if(current->left->left==NULL && current->left->right!=NULL){
Node *temp=new Node;
temp=current->left;
current->left=current->left->right;
cout<<"\nDeleting Node containing "<<temp->data<<endl;
delete temp;
}
else if(current->left->right==NULL && current->left->left!=NULL){
Node *temp=new Node;
temp=current->left;
current->left=current->left->left;
cout<<"\nDeleting Node containing "<<temp->data<<endl;
delete temp;
}
else if(current->left->left!=NULL && current->left->right!=NULL){

Node *temp=new Node;
temp=findMin(current->left->right);
cout<<"\nReplacing "<<current->left->data<< " with "<<temp->data;
current->left->data=temp->data;
cout<<"\nDeleting Node containing "<<temp->data<<endl;
delete temp;
}
}
else {
cout<<"\nMoving to left Node ";
deleteNode(current->left, d);
}
}
}
if(d>current->data){
 if(current->right!=NULL){
if(d==current->right->data){
if((current->right->left==NULL) && (current->right->right==NULL)){
cout<<"\nDeleting Node containing "<<current->right->data<<endl;
delete (current->right);
current->right=NULL;
}
else if(current->right->left==NULL && current->right->right!=NULL){
Node *temp=new Node;
temp=current->right;
current->right=current->right->right;
cout<<"\nDeleting Node containing "<<temp->data<<endl;
delete temp;
}
else if(current->right->right==NULL && current->right->left!=NULL){
Node *temp=new Node;
temp=current->right;
current->right=current->right->left;
cout<<"\nDeleting Node containing "<<temp->data<<endl;
delete temp;
}
else if(current->left->left!=NULL && current->left->right!=NULL){

Node *temp=new Node;
temp=findMin(current->right->right);
cout<<"\nReplacing "<<current->right->data<< " with "<<temp->data;
current->right->data=temp->data;
cout<<"\nDeleting Node containing "<<temp->data<<endl;
delete temp;
}
}
else {
cout<<"\nMoving to right Node ";
deleteNode(current->right, d);
}
 }
}
if(d==current->data){
if(current->right==NULL){
Node *temp=new Node;
temp=current;
current=current->left;
cout<<"\nDeleting Node containing "<<temp->data<<endl;
delete temp;
}
else if(current->left==NULL){
Node *temp=new Node;
temp=current;
current=current->right;
cout<<"\nDeleting Node containing "<<temp->data<<endl;
delete temp;
}
else if(current->right!=NULL && current->left!=NULL){
Node *temp=new Node;
temp=findMin(current->right);
current->data=temp->data;
cout<<"\nDeleting Node containing "<<temp->data<<endl;
delete temp;
}
}
}

void inOrder(Node *current){

if(current!=NULL){

inOrder(current->left);
cout<<current->data<<" ";
inOrder(current->right);
}
}

void preOrder(Node *current){

if(current!=NULL){

cout<<current->data<<" ";
inOrder(current->left);
inOrder(current->right);
}
}

void postOrder(Node *current){

if(current!=NULL){

inOrder(current->left);
inOrder(current->right);
cout<<current->data<<" ";
}
}
};


int _tmain(int argc, _TCHAR* argv[])
{
int d=0;
cout<<"\nSelect the root of your tree :";
cin>>d;
Tree t(d);
int option;

cout<<"Enter your option: \n1) Insert data: \n2) Inorder Traversal: \n3) Search: \n4) Preoder Traversal: \n5) Postorder Traversal:  \n6) Find Minimum: \n7) Delete Node: \n8) Press 0 to exit: ";
cin>>option;

while(option!=0){
switch(option){
case 1:
cout<< "\nEnter the data you want to insert: ";
cin>> d;
t.insert(t.getRoot(), d);
cout<<"\nThe root is "<< t.getRoot();
cout<<"\nThe current pointer is "<< t.getCurrent()<<endl;
break;

case 2:
t.inOrder(t.getRoot());
cout<<endl;
break;

case 3:{
cout<<"\nEnter the number you want to search :" ;
cin>>d;
Node *temp=new Node;
temp=t.search(t.getRoot(),d);
if(temp){
cout<<"\nThe number "<< temp->data<< " exists in the tree ";
}
else cout<<"\nThe number "<< d<<" does not exist in the tree ";
break;
}

case 4:
t.preOrder(t.getRoot());
break;

case 5:
t.postOrder(t.getRoot());
break;



case 6:{
Node *temp=new Node;
temp=t.findMin(t.getRoot());
cout<<temp->data<<endl;
break;
  }

case 7:{
cout<<"\nEnter the number you want to delete: ";
cin>>d;
t.deleteNode(t.getRoot(), d);
break;
  }
default:
cout<<"\nEnter a correct option: ";
}
cout<<"\nEnter your option: \n1) Insert data: \n2) Inorder Traversal: \n3) Search: \n4) Preoder Traversal: \n5) Postorder Traversal:  \n6) Find Minimum: \n7) Delete Node: \n8) Press 0 to exit: ";
cin>>option;
}
return 0;
}

Friday, August 22, 2014

Selection sort

#include <stdio.h>
 
int main()
{
   int array[100], n, c, d, position, swap;
 
   printf("Enter number of elements\n");
   scanf("%d", &n);
 
   printf("Enter %d integers\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);
 
   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;
 
      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }
 
   printf("Sorted list in ascending order:\n");
 
   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);
 
   return 0;
}

Bubble Sorting

#include <stdio.h>
 
int main()
{
  int array[100], n, c, d, swap;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d integers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
 
  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }
 
  printf("Sorted list in ascending order:\n");
 
  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);
 
  return 0;
}