You are on page 1of 10

Selection Sort

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,p,k,min,loc,temp;

cout<<"\n------------ SELECTION SORT ------------ \n\n";
cout<<"Enter No. of Elements=";
cin>>n;

cout<<"\nEnter Elements=\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}

for(p=1;p<=n-1;p++) // Loop for Pass
{
min=a[p]; // Element Selection
loc=p;

for(k=p+1;k<=n;k++) // Finding Min Value
{
if(min>a[k])
{
min=a[k];
loc=k;
}
}

temp=a[p]; // Swap Selected Element
and Min Value
a[p]=a[loc];
a[loc]=temp;

}

cout<<"\nAfter Sorting : \n";

for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}

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();
}


Bubble sort
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,p,j,temp;

cout<<"\n------------ BUBBLE SORT ------------ \n\n";

cout<<"Enter No. of Elements : ";
cin>>n;

cout<<"\nEnter Elements : \n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}


for(p=1;p<=n-1;p++) // Loop for Pass
{

for(j=1;j<=n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j]; // Interchange Values
a[j]=a[j+1];
a[j+1]=temp;
}
}

}

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();
}

Array insertion
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,item,loc,j;

cout<<"\n------------ INSERT ELEMENT INTO ARRAY ----------
-- \n\n";

cout<<"Enter No. of Elements : ";
cin>>n;

cout<<"\nEnter Elements : \n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}

cout<<"\nEnter Item you want to Insert : ";
cin>>item;

cout<<"\nEnter Location where you want to Insert Item : ";
cin>>loc;

j=n; // Initialize Counter

while(j>=loc)
{
a[j+1]=a[j]; // Move Element Downward
j--;
}

a[loc]=item; //Insert Element
n=n+1;

cout<<"\nArray after Insertion : \n";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}

Array deletion

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,item,loc,j;

cout<<"\n------------ DELETE ELEMENT FROM ARRAY ----------
-- \n\n";

cout<<"Enter No. of Elements : ";
cin>>n;

cout<<"\nEnter Elements : \n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}

cout<<"\nEnter Location of Element, you want to Delete :
";
cin>>loc;


item=a[loc];

for(j=loc;j<=n-1;j++)
{
a[j]=a[j+1]; // Move Elemets
Upward
}

n=n-1; // Reset the No. of
Elemets in Array

cout<<"\nArray after Deletion : \n";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}

cout<<"\nElement "<<item<<" is deleted";
getch();
}

Merging array

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

int a[100],b[100],c[100],n,m,i,j,k,s;

cout<<"------ Merging Two Array's ------";
cout<<"\n\nEnter No. of Elements in First Array : ";
cin>>n;

cout<<"\nEnter Elements in Sorted Order:\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}

cout<<"\nEnter No. of Elements in Second Array : ";
cin>>m;

cout<<"\nEnter Elements in Sorted Order:\n";
for(i=1;i<=m;i++)
{
cin>>b[i];
}

i=1,j=1;

for(k=1;k<=n+m;k++)
{
if(a[i]<b[j]) // Compare Elements of Array A and
Array B
{
c[k]=a[i];
i++;

if(i>n)
goto b;
}

else
{
c[k]=b[j];
j++;

if(j>m)
goto a;
}
}


a:
for(s=i;s<=n;s++) // Copy the Remaining Elements of
Array A to C
{
k++;
c[k]=a[s];
}


b:
for(s=j;s<=m;s++) // Copy the Remaining Elements of
Array B to C
{
k++;
c[k]=b[s];
}

cout<<"\n\nAfter Merging Two Arrays:\n";

for(k=1;k<=n+m;k++)
{
cout<<c[k]<<endl;
}

getch();
}

You might also like