Pages

Showing posts with label SORTING. Show all posts
Showing posts with label SORTING. Show all posts

Monday, May 2, 2011

MERGE SORT

#include<iostream.h>
#include<conio.h>
void mergesort(int[],int,int);
void merge(int[],int,int,int);
void main()
{
int a[10],p,q,r,i,n;
clrscr();
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];
}
getch();
}

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

Thursday, February 17, 2011

BUBBLE SORT

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
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";
getch();

}







In this short video we take a look at the bubblesort sorting algorithm. It's one of the worst performing sorting algorithms, but it is also the easiest to explain. We also look at bubblesorts cousin shakersort.

Wednesday, February 16, 2011

MERGING OF SORTED ARRAYS

#include<iostream.h>
#include<conio.h>
class merge
{
int i,j,k,m,n;
int a[10],b[10],c[20];
public:
void getar1();
void getar2();
void display();
};
void merge::getar1()
{
cout<<"\n"<<"enter size of 1st array (max 10)";
cin>>n;
cout<<"\n enter array";
for(i=1;i<=n;i++)
cin>>a[i];
}
void merge::getar2()
{
cout<<"\n enter size 2nd array (max 10)";
cin>>m;
cout<<"\n enter array";
for(j=1;j<=m;j++)
cin>>b[j];
}
void merge::display()
{
k=1; i=1;j=1;
cout<<"\n sorted array : \n";
while(i<=n&&j<=m)
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;i++;
}
else
{
c[k]=b[j];
k++;j++;
}
}
if(i>n)
{
while(j<=m)
{
c[k]=b[j];
k++;j++;
}
}
else
{
while(i<=n)
{
c[k]=a[i];
k++;i++;
}
}
for(k=1;k<=m+n;k++)
cout<<c[k]<<"\t";
}
void main()
{
clrscr();
cout<<"\t///////////// MERGING///////////////\n\n\n";
merge obj;
obj.getar1();
obj.getar2();
obj.display();
getch();
}

SELECTION SORT

#include<iostream.h>
#include<conio.h>
class selection_sort
{
int a[50];
int i,j,k,n,min,temp,l,pos;
public:
void getarray();
void display();
};
void selection_sort::getarray()
{
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];
}
void selection_sort::display()
{
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";
}
void main()
{
clrscr();
selection_sort obj;
cout<<"\t ///////////////////SELECTION SORT///////////////////";
obj.getarray();
cout<<" sorted array"<<"\n";
obj.display();
getch();
}



This time we take a look at how Selection Sort works

INSERTION SORT

HERE IS SIMPLEST WAY TO PERFORM INSERTION SORT

JUST COPY THE BELOW GIVEN CODE IN YOUR TC FOLDER AND ENJOY IT....


#include<iostream.h>
#include<conio.h>
class insertion_sort
{
int a[50];
int i,j,k,n,temp,l,m,pos;
public:
void getarray();
void display();
};
void insertion_sort::getarray()
{
cout<<"\nEnter the size of the array(max 50)"; cin>>n;
cout<<"\n Enter the array"; for(l=1;l<=n;l++) cin>>a[l];
}
void insertion_sort::display()
{
for(i=2;i<=n;i++)
{
temp=a[i];pos=i-1;
while(temp<a[pos])
{
a[pos+1]=a[pos];
pos--;
}
a[pos+1]=temp;
}


for(m=1;m<=n;m++)
cout<<a[m]<<"\t";
}
void main()
{
clrscr();
insertion_sort obj;
cout<<"\t ///////////////////INSERTION SORT///////////////////";
obj.getarray();
cout<<" sorted array"<<"\n";
obj.display();
getch();
}




We continue looking at sorting algorithms, this time we take a look at Insertion Sort. Insertion is better than bubble sort, but still very poorly behaving.