Professional Documents
Culture Documents
#include <iostream.h>
void selectionSort(int AR[],int size)
{
int i,j,temp;
for (i=0; i<size; i++)
{
for (j=i+1; j<=size; j++)
{
if ( AR[i] > AR[j] ) // sorts in ascending order
{
temp = AR[j];
AR[j] = AR[i];
AR[i] = temp;
}
} // end of sub loop
} // end of main loop
} // end of function selectionSort
int main()
{
int arr[20],size;
// prompts the user to enter the elements in the array
cout<<" Enter the no. of elements that you want to enter (max 20 ) : ";
cin>>size;
cout<<" Now enter the elements in the array ";
for (int i=0; i<size; i++)
{
cout<<" \n Element "<<i<<" : ";
cin>>arr[i];
}
selectionSort(arr,size); // calls the function to sort the array
cout<<" \n The sorted array is as follows ";
for (int i=0; i<size; i++)
{
cout<<" \n Element "<<i<<" : "<<arr[i];
}
return 0;
} // end of main
Bubble sort
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";
for(i=0;i<n;++i)
cin>>a[i];
for(i=0;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
cout<<"Array after bubble sort: ";
for(i=0;i<n;++i)
cout<<a[i]<<" ";
getch();
}
Insertion Sort
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,p,ptr,temp;
cout<<"\n------------ INSERTION SORT ------------ \n\n";
cout<<"Enter No. of Elements : ";
cin>>n;
cout<<"\nEnter Elements : \n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
a[0]=0;
for(p=2;p<=n;p++)
{
temp=a[p];
ptr=p-1;
while(temp<a[ptr])
{
a[ptr+1]=a[ptr]; // Move Element Forward
ptr--;
}
a[ptr+1]=temp; // Insert Element in Proper Place
}
cout<<"\nAfter Sorting : \n";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
Linear Search
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,item,s=0;
cout<<"\n------------ LINEAR SEARCH ------------ \n\n";
cout<<"Enter No. of Elements=";
cin>>n;
cout<<"\nEnter Elements=\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"\nEnter Element you want to Search=";
cin>>item;
for(i=1;i<=n;i++) //Array Elements
Comparsion with Item
{
if(a[i]==item)
{
cout<<"\nData is Found at Location : "<<i;
s=1;
break;
}
}
if(s==0)
{
cout<<"Data is Not Found";
}
getch();
}
Binary Search
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],n,i,beg,end,mid,item;
cout<<"\n------------ BINARY SEARCH ------------ \n\n";
cout<<"Enter No. of Elements= ";
cin>>n;
cout<<"\nEnter Elements in Sorted Order=\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"\nEnter Item you want to Search= ";
cin>>item;
beg=1;
end=n;
mid=(beg+end)/2; // Find Mid Location of
Array
while(beg<=end && a[mid]!=item) // Compare Item and Value of
Mid
{
if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
mid=(beg+end)/2;
}
if(a[mid]==item)
{
cout<<"\nData is Found at Location : "<<mid;
}
else
{
cout<<"Data is Not Found";
}
getch();
}
Implementation of Linked List
#include<iostream.h>
#include<conio.h>
struct Node
{
int info; //info variable will store data of a Node.
Node *link; //link pointer will store the address of
Next Node.
};
Node *start=NULL; //*start=NULL means Intially Linked List
is Empty.
void Insert();
void Delete();
void Search();
void Display();
void main()
{
clrscr();
int choice;
for(;;) //Infinite Loop.
{
cout<<"\n\n\n\n---------- Linked List ---------- \n";
cout<<"\nMain Menu\n\n1. Insert at Beginning\n
2. Delete from Beginning\n
3. Search\n
4. Display\n
5. Exit\n\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
Insert();
Display();
break;
case 2:
Delete();
Display();
break;
case 3:
Search();
break;
case 4:
Display();
break;
case 5:
return; //Exit
default:
cout<<"\nWrong Input";
}
}
}
void Insert() //Insert a New Node at the
Beginning of List.
{
Node *temp=new Node(); //Memory Allocation to a New
Node.
cout<<"\nEnter the Value : ";
cin>>temp->info;
temp->link=start;
start=temp;
}
void Delete() //Delete Starting Node from
List.
{
if(start==NULL)
cout<<"\nList is Empty.\n";
else
{
Node *temp=start;
start=start->link;
temp->link=NULL;
cout<<"\nItem "<<temp->info<<" is Deleted.\n";
}
}
void Search() //Search a Node from List.
{
int item,loc=1;
Node *temp=start;
cout<<"\nEnter Item you want to Search :";
cin>>item;
while(temp!=NULL)
{
if(item==temp->info)
{
cout<<"\nItem is Found at Location : "<<loc;
return;
}
else
{
temp=temp->link;
loc++;
}
}
cout<<"\nItem is Not Found";
}
void Display() //Display's the Elements in
List.
{
Node *temp=start;
cout<<"\nList is : ";
while(temp!=NULL)
{
cout<<temp->info<<" ";
temp=temp->link;
}
}
Implementation of Stack using Array
#include<iostream.h>
#include<conio.h>
#define max 10 //Maximum Size of Stack
int stack[max];
int top=-1,item; //top=-1 means Stack is Empty.
//First Element will be stored at Zero
Location. (top=0)
void main()
{
clrscr();
int choice;
void Push();
void Pop();
void Display();
for(;;) //Infinite Loop.
{
cout<<"\n\n\n\n---------- STACK ---------- \n";
cout<<"\nMain Menu\n\n1. PUSH\n2. POP\n3. Display\n4.
Exit\n\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Display();
break;
case 4:
return; //Exit
default:
cout<<"\nWrong Input";
}
}
}
void Display() //Display's the Element of Stack.
{
int i;
if(top==-1)
{
cout<<"\nStack is Empty.\n";
}
else
{
for(i=top;i>=0;i--)
{
cout<<endl<<"Stack["<<i<<"]="<<stack[i];
if(i==top)
cout<<" <-- Top";
}
}
}
void Push() //Insert a New Element in Stack.
{
if(top==max-1)
{
cout<<"\nOverflow\n";
}
else
{
cout<<"\nEnter the Element you want to Insert : ";
cin>>item;
top=top+1;
stack[top]=item;
cout<<endl<<item<<" is Inserted at Top.\n";
Display();
}
}
void Pop() //Delete Element from Stack.
{
if(top==-1)
{
cout<<"\nUnderflow\n";
}
else
{
item=stack[top];
top=top-1;
cout<<endl<<item<<" is Deleted from Top.\n";
Display();
}
}
Implementation of Queue using Array
#include<iostream.h>
#include<conio.h>
#define size 5 //Maximum Size of Queue
int Queue[size];
int rear=-1,front=-1,item; //rear=-1, front=-1 means Queue
is Empty.
//First Element will be stored at
Zero Location.
//(rear=0, front=0)
void main()
{
clrscr();
int choice;
void Insert();
void Delete();
void Display();
for(;;) //Infinite Loop.
{
cout<<"\n\n\n\n---------- QUEUE ---------- \n";
cout<<"\nMain Menu\n\n1. Insert\n2. Delete\n3. Display\n4.
Exit\n\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
Insert();
break;
case 2:
Delete();
break;
case 3:
Display();
break;
case 4:
return; //Exit
default:
cout<<"\nWrong Input";
}
}
}
void Display() //Display's the Element of
Queue.
{
int i;
if(front==-1)
cout<<"\nQueue is Empty\n";
else
for(i=front;i<=rear;i++)
cout<<endl<<"Queue["<<i<<"]="<<Queue[i];
}
void Insert() //Insert a New Element in
Queue.
{
if(rear==size-1)
cout<<"\nOverflow\n";
else
{
if(rear==-1 && front==-1)
rear=0,front=0;
else
rear++;
cout<<"\nEnter the Element you want to Insert : ";
cin>>item;
Queue[rear]=item;
cout<<endl<<item<<" is Inserted.\n";
Display();
}
}
void Delete() //Delete Element from Queue.
{
if(front==-1)
cout<<"\nUnderflow\n";
else
{
item=Queue[front];
if(front==rear)
front=-1,rear=-1;
else
front=front+1;
cout<<endl<<item<<" is Deleted.\n";
Display();
}
}
Array (Insertion, Deletion)
#include<iostream.h>
#include<conio.h>
int a[100],i,n=0,item,pos,j;
void main()
{
clrscr();
int choice;
void Ins_end();
void Ins_pos();
void Del_val();
void Del_pos();
void Display();
while(1) //Infinite Loop.
{
cout<<"\n\n\n\n---------- Array ---------- \n";
cout<<"\nMain Menu\n\n1. Insert an Elemnet\n
2. Insert an Element at given Position\n
3. Delete an Element by given Value\n
4. Delete an Element from given Position\n
5. Exit\n\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
Ins_end();
break;
case 2:
Ins_pos();
break;
case 3:
Del_val();
break;
case 4:
Del_pos();
break;
case 5:
return; //Exit
default:
cout<<"\nWrong Input";
}
}
}
void Display() //Display Elemnets of Array.
{
for(i=1;i<=n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<endl;
}
}
void Ins_end() //Insert a New Elemnet at the End of
Array.
{
cout<<"\nEnter Element : ";
cin>>item;
n=n+1;
a[n]=item;
cout<<"\n\nAfter Insertion :\n\n";
Display();
}
void Ins_pos() //Insert a New Elemnet at given
Position.
{
cout<<"\nEnter Element : ";
cin>>item;
cout<<"\nEnter Position : ";
cin>>pos;
if(pos<1 || pos>n+1)
{
cout<<"\nInvalid Position.\n\n";
return;
}
for(j=n;j>=pos;j--)
{
a[j+1]=a[j];
}
a[pos]=item;
n=n+1;
cout<<"\n\nAfter Insertion :\n\n";
Display();
}
void Del_val() //Delete a Elemnet whose value is
given.
{
int s=0;
cout<<"\nEnter Value you want to Delete : ";
cin>>item;
for(i=1;i<=n;i++)
{
if(a[i]==item)
{
s=i;
break;
}
}
if(s==0)
{
cout<<"\nThe given value is not in the Array.\n\n";
return;
}
for(j=s+1;j<=n;j++)
{
a[j-1]=a[j];
}
n=n-1;
cout<<"\n\nAfter Deletion :\n\n";
Display();
}
void Del_pos() //Delete a Elemnet whose position is
given.
{
cout<<"\nEnter Position : ";
cin>>pos;
if(pos<1 || pos>n)
{
cout<<"\nInvalid Position.\n\n";
return;
}
for(j=pos+1;j<=n;j++)
{
a[j-1]=a[j];
}
n=n-1;
cout<<"\n\nAfter Deletion :\n\n";
Display();
}
Implementation of Circular Queue using Array
#include<iostream.h>
#include<conio.h>
#define size 5 //Maximum Size of Queue
int Queue[size];
int rear=-1,front=-1,item; //rear=-1, front=-1 means Queue
is Empty.
//First Element will be stored at
Zero Location.
//(rear=0, front=0)
void main()
{
clrscr();
int choice;
void Insert();
void Delete();
void Display();
for(;;) //Infinite Loop.
{
cout<<"\n\n\n\n---------- CIRCULAR QUEUE ---------- \n";
cout<<"\nMain Menu\n\n1. Insert\n2. Delete\n3. Display\n4.
Exit\n\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
Insert();
break;
case 2:
Delete();
break;
case 3:
Display();
break;
case 4:
return; //Exit
default:
cout<<"\nWrong Input";
}
}
}
void Display() //Display's the Element of
Queue.
{
int i;
if(front==-1)
cout<<"\nQueue is Empty\n";
else if(front>rear)
{
for(i=0;i<=rear;i++)
cout<<endl<<"Queue["<<i<<"]="<<Queue[i];
for(i=front;i<=size-1;i++)
cout<<endl<<"Queue["<<i<<"]="<<Queue[i];
}
else
for(i=front;i<=rear;i++)
cout<<endl<<"Queue["<<i<<"]="<<Queue[i];
cout<<"\n\nFront : "<<front;
cout<<"\nRear : "<<rear<<endl;
}
void Insert() //Insert a New Element in
Queue.
{
if((front==0 && rear==size-1) || (front==rear+1))
cout<<"\nOverflow\n";
else
{
if(rear==-1 && front==-1) //First Element in Queue.
rear=0,front=0;
else if(rear==size-1) //If rear reached at End then
set rear to Beginning.
rear=0;
else
rear++;
cout<<"\nEnter the Element you want to Insert : ";
cin>>item;
Queue[rear]=item;
cout<<endl<<item<<" is Inserted.\n";
Display();
}
}
void Delete() //Delete Element from Queue.
{
if(front==-1)
cout<<"\nUnderflow\n";
else
{
item=Queue[front];
if(front==rear) //In case of Single Element in
the Queue.
front=-1,rear=-1;
else if(front==size-1) //If front reached at End then
set front to Beginning.
front=0;
else
front=front+1;
cout<<endl<<item<<" is Deleted.\n";
Display();
}
}