Selection Sort

This type of sorting is called "Selection Sort" because it works by repeatedly element. It works as follows: first find the smallest in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.

The program is written below:

#include<iostream>
using namespace std;
int main()
{
int a[50];
int i,j,k,n,min,temp,l,pos;
cout<<"nEnter the size of the array(max 50)"; cin>>n;
cout<<"n Enter the array";
for(i=1;i<=n;i++) { cin>>a[i];
}
for(k=1;k<=n;k++)
{
min=a[k];
for(l=k+1;l<=n;l++) { if(min>a[l])
{
min=a[l];
pos=l;
}
}
temp=min;
a[pos]=a[k];
a[k]=temp;
}
for(int j=1;j<=n;j++)
{
cout<<a[j]<<"t";
}
return 0;
}

Output:
Screenshot from 2013-09-29 22:50:22

Bubble Sort

The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions. Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.

The code for the sort is as follows:

#include<iostream>
using namespace std;
int main()
{
int a[50],n,i,j,temp,l;
cout<<"enter the size of array (max 50)";
cin>>n;
cout<<"n enter the array";
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"n sorted array is";
for(j=1;j<=n-1;j++)
{
for(int k=j+1;k<=n;k++)
{
if(a[j]>a[k])
{
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
}
for(l=1;l<=n;l++)
cout<<a[l]<<"t";
return 0;
}

Output:
Screenshot from 2013-09-29 18:59:45

Merge Sort

The mergesort algorithm is based on the classical divide-and-conquer paradigm. It operates as follows:

DIVIDE: Partition the n-element sequence to be sorted into two subsequences of n/2 elements each.

CONQUER: Sort the two subsequences recursively using the mergesort.

COMBINE: Merge the two sorted sorted subsequences of size n/2 each to produce the sorted sequence consisting of n elements.

The code for its program is as follows:

#include<iostream>
using namespace std;
void mergesort(int[],int,int);
void merge(int[],int,int,int);
int main()
{
int a[10],p,q,r,i,n;
cout<<"Enter the number of elements:";
cin>>n;
p=0;
r=n-1;
cout<<"Enter the array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
mergesort(a,p,r);
cout<<"The sorted array is:";
for(i=0;i<n;i++)
{
cout<<"n"<<a[i];
}
return 0;
}
void mergesort(int a[],int p,int r)
{
if( p < r)
{
int q=(p+r)/2;
mergesort(a,p,q);
mergesort(a,q+1,r) ;
merge(a,p,q,r);
}
}
void merge(int a[],int p,int q,int r)
{
int c[10];
int i=p;
int j=q+1;
int k=p;
while((i<=q)&&(j<=r))
{
if(a[i]<a[j])
{
c[k]=a[i];
i=i+1;
k=k+1;
}
else
{
c[k]=a[j];
j=j+1;
k=k+1;
}
}
while(i<=q)
{
c[k] =a[i];
i=i+1;
k=k+1;
}
while(j<=r)
{
c[k]=a[j];
j=j+1;
k=k+1;
}
int l=p;
while(l<=r)
{
a[l]=c[l];
l=l+1;
}
}

Output:
Screenshot from 2013-09-29 19:46:12