void enternumber(int *array,int N); void bubblesort(int *array,int N, int Order); void selectionsort(int *array,int N, int Order); void insertionsort(int *array,int N, int Order); void quicksort(int *array,int left, int right, int N,int Order); int split(int lower, int upper,int *array,int Order); void displayarray(int *array, int N); //function prototype void main() { int n; int* array; int order, sorting=0; cout<<"Select (1) for ascending, (2) for descending order: "; cin>>order; while (order != 1 && order != 2) { cout<<"Input Error"<<endl; cout<<"Select (1) for ascending, (2) for descending order: "; cin>>order; } //prompt error message if input is other than required cout<<"Enter list of size: "; cin>>n; //get the size of array array=new int[n]; // creat an array with the size selected by the user enternumber(array,n); //function call for user to key in a series of //number depends to the size selected previously cout<<"Select Sorting techniques:\n"; cout<<"Select (1) for Bubble\nSelect (2) for Selection\n"; cout<<"Select (3) for Insertion\nSelect (4) for quick sort\n"; cin>>sorting; while ((sorting != 1) && (sorting != 2) && (sorting !=3) && (sorting !=4 ) ) { cout<<"Input Error"<<endl; cout<<"Select Sorting techniques:\n"; cout<<"Select (1) for Bubble\nSelect (2) for Selection\n"; cout<<"Select (3) for Insertion\nSelect (4) for quick sort\n"; cin>>sorting; }//prompt error message if input is other than required cout<<"Your numbers are: "; displayarray(array, n); //function call to display the array cout<<"Your choice of sorting is: "<<sorting<<endl; if (sorting==1) bubblesort(array,n,order); //function call for bubble sort else if (sorting==2) selectionsort(array,n,order);//function call for selection sort else if (sorting==3) insertionsort(array,n,order);//function call for insertion sort else { quicksort(array,0,n-1,n,order); //function call for quick sort cout<<"Sorted list for Quick sort: "; displayarray(array, n);//function call to display the array } } void enternumber(int *array,int N) { int i; for (i=0;i<N;i++) //repeat for N-1 times i.e. size-1 times { cout<<"Enter number: "; cin>>array[i]; } } void bubblesort(int *array,int N, int Order) { int i,j,temp=0; //local declaration for(i=0;i<N-1;i++) for(j=1;j<N;j++) //repeat the process for N-1 times i.e. size-1 times { if((array[j]<array[j-1])&&(Order==1))//ascending order {//perform if value in smaller index is more than larger index temp=array[j]; array[j]=array[j-1]; array[j-1]=temp; //swap array position } else if ((array[j]>array[j-1])&&(Order==2))//descending order { //perform if value in smaller index is lesser than lar ger index temp=array[j]; array[j]=array[j-1]; array[j-1]=temp;//swap array[j] with array[j-1] } } cout<<"Sorted list for Bubble sort: "; displayarray(array, N);//function call to display the array } void selectionsort(int *array,int N, int Order) { int i,j,min,temp=0; //local declaration for(i=0;i<N-1;i++) //repeat the process for N-2 times i.e. size-2 times { min=i;//let array[i] assume to be minimun for(j=i+1;j<N;j++) //repeat the process for N-1 times { if((array[j]<array[j-1]) &&(Order==1)) //ascending order min=j; //perform if value in smaller index is mo re than larger index else if((array[j]>array[j-1]) && (Order==2))//desending order min=j; //perform if value in smaller index is le sser than larger index } temp=array[min]; array[min]=array[i]; array[i]=temp;//swap array[i] with array[min](defined to be less er/minimum) } cout<<"Sorted list for Selection sort: "; displayarray(array, N);//function call to display the array } void insertionsort(int *array,int N, int Order) { int i,j,temp=0; //local declaration for (i=1;i<N;i++) //repeat the process for N-1 times i.e. size-1 times { temp=array[i]; j=i-1; //let index j equal to one less than index i if(Order==1)//ascending order { while(array[j]>temp) //while lower index is larger than higer index { array[j+1]=array[j]; //lower index's value t/ove r higher index position j--; } } else if (Order==2)//desending order { while((array[j]<temp) && (j>=0))//while lower index is s maller than higher {// index but lower index is not less than 0 array[j+1]=array[j];//higher index's value t/ove r lower index position j--; } } array[j+1]=temp; } cout<<"Sorted list for Insertion sort: "; displayarray(array, N);//function call to display the array } void quicksort(int *array,int left, int right,int N,int Order) { if (right>left) { int pivot=split(left, right,array,Order);//function call find pi vot quicksort(array,left,pivot-1,N,Order);//recursion quicksort(array,pivot+1,right,N,Order);//recursion } } int split(int left, int right,int *array, int Order) { int pivot=array[left]; if( Order==1)//ascending order { while(true) { while(array[left]<pivot) left++; while(array[right]>pivot) right--; if(left<right) swap(array[left],array[right]);//swap array else return right; } } else if (Order==2)//desending order { while(true) { while(array[left]>pivot) left++; while(array[right]<pivot) right--; if(left<right) swap(array[left],array[right]);//swap array else return right; } } } void displayarray(int *array,int N) { int i; for (i=0; i<N;i++) //repeat for N-1 times i.e. size-1 times cout<<array[i]<<" "; cout<<"\n"; }