sorting of random array by all techniques

kisi k pass random array ki sorting ka code hy by heap insertion quick nd selection sort...........
kindly help me
Last edited on
I don't understand. Could you please spell out the words?
anyone have a code that sort a random generated array by these sorting techniques in single program.
Since this is probably homework... you should do it! :)
i have zero understanding for sorting methods......
Well this is a problem that can usually be solved by studying! :)
#include <conio.h>
#include <ctime>
#include <iostream>

using namespace std;
class Sorting{
private:
int size,i,j,p,q,r,n;
int arr[];
public:
void bsort(int arr[])
{
int temp;
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<<" Sorted Array is: "<<endl;
for(int i=0;i<size;i++)
cout<<arr[i]<<endl;
getch();
}//bubble sort
//////////////////////////////////////////////////////////////////////////////////////
void selsort(int arr[])//selection sort
{
for(int i=0;i<size-1;i++)
{
int min=i;
int temp;
for(int j=i+1;j<size;j++)
if(arr[j]<arr[min])
{
min =j;
}
if(min!=i)
{
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
cout<<" Sorted array:\n";
for(int i=0;i<size;i++)
cout<<arr[i]<<endl;
getch();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
void quicksort(int arr[],int i,int j)//Quick sort
{
int left, right;
//int i=left,j=right;
int temp;
int pivot=arr[(left + right)/2];
while(i<=j)
{
while(arr[i]<pivot)
i++;
while(arr[j]>pivot)
j--;
if(i>=j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
break;
}
}

if(left<j)
quicksort(arr,left,j);
if(i<right)
quicksort(arr,i,right);
cout<<"sorted Array is\n";
for(int i=0;i<size;i++)
cout<<arr[i]<<endl;
}
//////////////////////////////////////////////////////////////////////////////////////
void insertsort(int arr[],int size)//insertion sort
{
int j,temp;
for (int i=1;i<size;i++)
{
j=i;
while(j>0&&arr[j]<arr[j-1])
{
temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
j--;
}
}
cout<<"sorted array is:";
for(int i=0;i<size;i++)
cout<<arr[i]<<endl;
}
////////////////////////////////////////////////////////////////////////////////
void mergesort(int arr[],int,int)
{
if( p < r)
{
int q=(p+r)/2;
mergesort(arr,p,q);
mergesort(arr,q+1,r) ;
merge(arr,p,q,r);
}
}
void merge(int[],int,int,int)
{
int c[10];
int i=p;
int j=q+1;
int k=p;
while((i<=q)&&(j<=r))
{
if(arr[i]<arr[j])
{
c[k]=arr[i];
i=i+1;
k=k+1;
}
else
{
c[k]=arr[j];
j=j+1;
k=k+1;
}
}
while(i<=q)
{
c[k] =arr[i];
i=i+1;
k=k+1;
}
while(j<=r)
{
c[k]=arr[j];
j=j+1;
k=k+1;
}
int l=p;
while(l<=r)
{
arr[l]=c[l];
l=l+1;
}
}
void display()
{

}


};
///////////////////////////// main function///////////
int main()
{
int size,i,j,p,q;
int choice;
Sorting s;
cout<< "how big do you want the array?" << endl;
cin >> size;



int arr[size];

srand((unsigned)time(0));

for(int i=0; i<size; i++){
arr[i] = (rand()%100)+1;

cout << arr[i] << endl;
}
cout<<"\nEnter 1.Bubble sort:\nEnter 2.Selection sort:\nEnter 3.Quicksort\nenter 4.Insertion sort\nEnter 5.Merg sort\nEnter 6. exit\n";
cin>>choice;
switch(choice)
{
case1:
s.bsort(arr);
getch();
break;
case2:
s.selsort(arr);
s.display();
getch();
break;
case3:
s.quicksort(arr,i,j);
s.display();
getch();
break;
case4:
s.insertsort(arr,size);
s.display();
break;
case5:
s.mergesort(arr,p,q);
s.display();
break;
case6:
exit(0);
break;


} }
check my program plzzzz and correct it.....
Topic archived. No new replies allowed.