Common sort algorithm implementations (direct insert sort, bubble sort, quick sort, build big selection Sort), and several next time write on.
#include "stdio.h" #include "stdlib.h" #include "malloc.h" #include <iostream>using namespace std; #define &NBSP;N&NBSP;10VOID&NBSP;SHOWARR (int a[]); Void insertsort (Int a []); //directly into sort Void bubblesort (int a[]); //bubble sort void quicksort (int a[],int ,int); //Quick sort Void simpleselectionsort (int a[]); //Simple Selection sort (select one with first interchange at a time) Int main () { int a[n] ={3,1,14,5,2,56,67,54,77,34}; showarr (a);// insertsort (a); //Direct Insert Sort// bubblesort (a); //bubble Sort //quicksort (a,0,n-1); //Quick Sort //cout<< " This is the result of the direct insert Sort "<<endl; //showarr (a); //cout<<endl; simpleselectiOnsort (a); system ("pause"); return 0;} Print List Void showarr (Int a[n]) { for (int i=0;i<n;i++) cout<<a[i]<< " "; cout<<endl;} Direct Insert sort Void insertsort (Int a[n]) { int tmp; int j; for (int i=1;i<n;i++) if (A[i]<a[i-1]) { tmp=a[i]; //this value to be present in TMP for (j=i-1;tmp<a[j];j--) a[j+1]=a[j]; //Front override value a[j+1]=tmp; } cout< < "This is the result of the direct insertion sort" <<endl; showarr (a); cout<<endl;} Bubble sort Void bubblesort (Int a[n]) { int tmp; for (int i=0;i<n;i++) for (int j=0;j<n-i-1;j++) { if (a[j]>a[j+1]) { tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp; } } cout <<endl<< "This is the result of the bubbling Sort" <<endl; showarr (a); cout <<endl;} Quick Sort Void quicksort (int a[],int left,int right) { if (left<right) { int i=left,j=right,tmp=a[i];// Each time with the first element as a base point, small in front, large in the back while (i<j) { while (i<j &NBSP;&&&NBSP;A[J]>TMP) j--; if (I<J) a[i++]=a[j]; while (I<J&NBSP;&&&NBSP;A[I]<=TMP) i++; if (I<J) a[j--]=a[i]; } a[i]=tmp; // Complete the first quick order and put it in its final position quicksort (a,left,i-1); //before the same method recursively quickly sort quicksort (a,i+1, right); //followed by the same method recursive quick sort }}//simple select sort void simpleselectionsort (int a[]) { int min; int tmp; //Temp Variable &Nbsp; for (int i=0;i<n-1;i++) { min=i; //first tentative min for the beginning of that element for (int j=i+1;j<n;j++) if (A[j]<a[min]) min=j; //find the smallest value tmp=a[min]; //exchange with the first element a[ min]=a[i]; a[i]=tmp; } cout<<endl<< "This is the result of the selection sort" <<endl; showarr (a); cout<<endl;}
Operation Result:
3 1 14 5 2 56 67 54 77 34 This is the result of selecting Sort 1 2 3 5 14 34 54 56 67 77 Press any key to continue ...
Common sort algorithm implementation (direct insert sort, bubble sort, quick sort, build large selection sort)