Category Archives: Sorting Techniques(code)

Insertion Sort :

<br />#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int a[10],n,j,temp;
cout<<"--------Insertion Sort---------"<<endl<<endl;
cout<<"Enter the size of array:"<<endl;
cin>>n;
cout<<"Enter Elements:"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
temp = a[i];
j=i-1;
while((temp < a[j]) && (j>=0))
{
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
cout<<endl<<"After Sorting Array is:"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl<<" @Myprogworld...!!"<<endl;
getch();
return 0;
}

Output :-

insertion sort

Shell Sort :

<br />#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int a[10],n,flag,gap;
cout<<"--------Shell Sort---------"<<endl<<endl;
cout<<"Enter the size of array:"<<endl;
cin>>n;
cout<<"Enter Elements:"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
gap=n/2;
do
{
do
{
flag=0;
for(int i=0;i<n-gap;i++)
{
if(a[i]>a[i+gap])
{
int temp = a[i];
a[i]=a[i+gap];
a[i+gap]=temp;
flag=1;
}
}
}while(flag);
}while(gap=gap/2);
cout<<endl<<"After Sorting Array is:"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl<<" @Myprogworld...!!"<<endl;
getch();
return 0;
}

Output :-
Shell sort

Selection Sort :

<br />#include<iostream>
#include<conio.h>
using namespace std;
int smallest(int a[],int k,int n)
{
int pos=k,small=a[k],i;
for(i=k+1;i<n;i++)
{
if(a[i]<small)
{
small=a[i];
pos=i;
}
}
return pos;
}
void selection_sort(int a[],int n)
{
int k,pos,temp;
for(k=0;k<n;k++)
{
pos=smallest(a,k,n);
temp=a[k];
a[k]=a[pos];
a[pos]=temp;
}
}
int main()
{
int a[10],n,i,j,k;
cout<<"Enter the size of the array:"<<endl;
cin>>n;
cout<<"Enter the elements:-"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
selection_sort(a,n);
cout<<"The Sorted array is:-"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<endl<<"Thanks...@Myprogworld..!!"<<endl;
getch();
return 0;
}

corollary:-
ssc

Quick Sort (p):

<br />#include<iostream>
#include<conio.h>
using namespace std;

int partition(int a[],int beg,int end)
{
int loc=beg,temp;
while(1)
{
while(a[loc]<=a[end] && (loc!=end))
{
end=end-1;
}
if(loc==end)
return loc;
else if(a[loc]>a[end])
{
temp=a[loc];
a[loc]=a[end];
a[end]=temp;
loc=end;
}
while(a[loc]>=a[beg] && loc!=beg)
{
beg=beg+1;
}
if(loc==beg)
return loc;
else if(a[loc]<a[beg])
{
temp=a[loc];
a[loc]=a[beg];
a[beg]=temp;
loc=beg;
}
}
}

void quick_sort(int a[],int beg,int end)
{
int loc;
if(beg<end)
{
loc=partition(a,beg,end);
quick_sort(a,beg,loc-1);
quick_sort(a,loc+1,end);
}
}

int main()
{
int a[10],n;
cout<<"Enter the size of array:"<<endl;
cin>>n;
cout<<endl<<"Enter elements here:"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
quick_sort(a,0,n-1);
cout<<endl<<"After sorting array is:"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"\t\t@Myprogworld";
getch();
return 0;
}

corollary :-
QSC

Merge Sort :

<br />#include<iostream>
#include<conio.h>
using namespace std;
void merge(int *,int,int,int);
void merge_sort(int *arr,int beg, int last)
{
int mid;
if(beg < last)
{
mid = (beg+last)/2;
merge_sort(arr,beg,mid);
merge_sort(arr,mid+1,last);
merge(arr,beg,mid,last);
}
return;
}
void merge(int *arr,int beg, int mid, int last)
{
int i=beg, j=mid+1,ind=beg,temp[10],k;
while((i <= mid) && ( j<= last))
{
if(arr[i] < arr[j])
{
temp[ind]=arr[i];
ind++;
i++;
}
else
{
temp[ind]=arr[j];
j++;
ind++;
}
}
while(j<=last)
{
temp[ind]=arr[j];
j++;ind++;
}
while(i<=mid)
{
temp[ind]=arr[i];
i++;
ind++;
}
for(k=beg;k<ind;k++)
{
arr[k]=temp[k];
}
}

int main()
{
int arr[10],i,n;
cout<<"Enter the size of the array: "<<endl;
cin>>n;
cout<<"Enter the elements of the array:"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<endl<<"Unsorted Array is :"<<endl;
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
merge_sort(arr,0,n-1);
cout<<endl<<"The sorted array is:"<<endl;
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
getch();
}

Output :-

Merge sort

Bubble sorting….!!!

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int arr[10],num,size,temp;
cout<<"Enter the size of array:-"<<endl;
cin>>size;
cout<<"Enter the elements in array:-"<<endl;
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
cout<<"Array before sorting is:-"<<endl;
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
//--------------------sorting-----------------------
for(int i=0;i<size;i++)
{
for(int j=0;j<size-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<endl<<"After sorting array is:-"<<endl;
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl<<"Thank you......@ Myprogworld.!!"<<endl;
getch();
return 0;
}

Output :-

Untitled