#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
using namespace std;
/////////////////// ProtoTypes ////////////////////////////////
void BubbleSort(int Arr1[]);
void shellSort(int Arr2[]);
void selectionSort(int Arr3[]);
void merge_sort(int Arr1[],int Arr2[], int Arr3[]);
////////////////// Main Driver //////////////////////////////
int main()
{
cout<<"THIS IS THE MAIN ARRAYOF 150:"<<endl;
int Arr[150], Arr1[50], Arr2[50], Arr3[50], Tmp1[100], Tmp2[150];
srand ((unsigned) time(NULL));
for (int i=0; i<150 ;i++)
{
Arr[i]=rand()%1000;
cout<< " "<<Arr[i];
}
//////////////////////////////// BUBBLE SORT////////////////////////////////
cout <<endl<<endl<<endl;
cout<<" 1 A...THIS IS THE FIRST UNSORTED 50 ELEMENTS OF THE ARRY "<<endl;
for(int i=0; i < 49; i++)// this is
{
Arr1[i] = Arr[i];
cout <<" "<<Arr1[i];
}
BubbleSort(Arr1);
cout <<endl<<endl<<" 1 B...THIS THE THE BUBBLE SORTED ELEMENTS OF ARRAY 1:"<<endl;
for(int i=0;i<49;i++)
{ cout<<" "<<Arr1[i]; }// this format for paper space
///////////////////////////////SHELL SORT////////////////////////
cout<<endl<<endl<<endl<<endl<<"2 A...THIS IS THE SECOND 50 ELEMENTS OF THE ARRAY "<<endl<<endl;
for(int i=50; i < 99; i++)
{
Arr2[i-50] =Arr[i];
cout <<" "<<Arr2[i];
}
cout<<endl<<endl<<"2 B...THIS THE THE SHELL SORTED ARRAY OF 50 ELEMENTS"<< endl;
shellSort(Arr2);
for(int i=0; i<50-1;i++)
{ cout<<" "<<Arr2[i]; }
///////////////// Selction Sort ///////////////////////////////
cout<<endl<<endl<<endl<<endl<<" 3 A ... THIS IS THE THIRD 50 ELEMENTS OF THE ARRAY "<<endl;
for(int i=100; i < 149-1; i++)
{
Arr3[i-100] =Arr[i] ;
cout <<" "<<Arr3[i];
}
cout<<endl<<endl<<"3 B...THIS THE THE SELECTION SORTED ARRAY OF 50 ELEMENTS"<< endl;
selectionSort(Arr3);
for(int i=0; i<50-1;i++)
{
cout<<" "<<Arr3[i];
}
//////////////// Merge Sort ///////////////////////////////
cout<<endl<<endl<<endl<<endl<<" 4 A ... THIS IS THE MERGE OF ALL ELEMENTS OF EACH ARRAY "<<endl;
merge_sort(Arr1, Arr2, Arr3);
for(int i =0; i<100-1; i++)
{
cout<<" "<< Arr3[i];
}
system("PAUSE");
return EXIT_SUCCESS;
}
///////////////////// FREEE FUNCTIONS /////////////////////////////////////
void BubbleSort(int Arr1[])
{
int tmp;
for(int i=0;i<49-1;i++)
{
for(int x=0; x<49-1; x++)
{
if(Arr1[x] > Arr1[x+1])
{
tmp = Arr1[x];
Arr1[x] = Arr1[x+1];
Arr1[x+1] = tmp;
} } }
}
void shellSort(int Arr2[])
{
int i,j, hCnt,h,k ,increments[20];
for(h=1, i=0;h<49; i++)
{
increments[i]=h;
h=3*h+1;
}
for(i--;i>=0;i--)
{
h=increments[i];
for(hCnt = h; hCnt<2*h;hCnt++)
{
for(j= hCnt; j<49;)
{
int tmp = Arr2[j];
k=j;
while(k-h>=0 && tmp<Arr2[k-h])
{
Arr2[k] = Arr2[k-h];
k-=h;
}
Arr2[k]=tmp;
j+=h;
}}}}
void selectionSort(int Arr3[])
{
int tmp;
int min;
for(int i=0;i<50;i++)
{
min = i;
for(int x=i; x<50; x++)
{
if(Arr3[x] < Arr3[min])
{
min = x;
}
}
tmp = Arr3[i];
Arr3[i] = Arr3[min];
Arr3[min] = tmp;
}
}
void merge_sort(int Arr1[],int Arr2[], int Arr3[])
{
int Tmp1[100];// this is s tem []
int Tmp2[150];// ditto
int i;
while((Arr1 < 50) && (Arr2 < 50))
{
if (Arr1[i] < Arr2[i])
{
Tmp1[i] = Arr1[i];
Arr1[i]++; //increase the index
cout<<" "<<Arr1;
}
else
{
Tmp1[i] = Arr2[i];
Arr2[i]++; //increase the index
cout<<" "<<Arr2;
}
Tmp1[i]++; //move to the next position in the new array
}
// Push remaining elements to end of new array when 1 feeder array is empty
while (Tmp1[i] <100)
{
Tmp1[i] = Arr1[i];
Arr1[i]++;
Arr3[i]++;
}
while (Arr2[i] < 100)
{
Tmp1[i] = Arr2[i];
Arr2[i]++;
Arr3[i]++;
}
return;
}