1. Merge sort
void merge (int *result, int begin, int mid, int end, int *temp) {int k = Begin;int i = begin;int J = mid + 1;while (i <= Mid&&j <= end) {temp[k++] = Result[i] < Result[j]? result[i++]: result[j++];} while (I <= mid) temp[k++] = Result[i++];while (J <= end) temp[k++] = result[j++];for (int t = begin; t <= end; t++ ) result[t] = temp[t];} void mergesort (int *result, int begin, int end, int *temp) {if (Begin < end) {int mid = (End-begin)/2 + Begin;mergeso RT (result, begin, Mid, temp); MergeSort (result, mid + 1, end, temp); Merge (result, begin, Mid, end, temp);}}
2. Quick Sort
int partition (int *result, int begin, int end) {int temp = Result[begin];int Low = Begin;int High = end;while (Low < HIG h) {while (Low < High&&result[high] > Temp) high--;if (Low < high) result[low++] = Result[high];while (Low & Lt High&&result[low] < Temp) low++;if (Low < high) result[high--] = Result[low];} Result[low] = Temp;return low;} void QuickSort (int *result, int begin, int end) {if (Begin < end) {int k = partition (result, begin, end); QuickSort (Result , begin, K-1); QuickSort (result, k + 1, end);}}
3. Insert Sort
void Insertsort (int *a, int length) {for (int i = 1; i < length; i++) {int temp = A[i];int J = i-1;while (J >= 0 &am p;& A[j] > Temp) {a[j + 1] = a[j];j--;} A[j + 1] = temp;}}
4. Bubble sort
void Bubblesort (int *a, int length) {for (int i = length-1; I >= 0; i--) {for (int j = 0; J < i; J + +) {if (a[j]>a[ J + 1]) {//swap (A[j], a[j + 1]); int temp = A[j];a[j] = a[j + 1];a[j + 1] = temp;}}}
5. Hill sort
void Shellsort (int *a, int length) {for (int i = LENGTH/2; I >= 1; i = I/2) {for (int j = i; j < length; J + +) {int K = J-i;int temp = a[j];while (k >= 0 && a[k] > Temp) {a[k + i] = a[k];k-= i;} A[k + i] = temp;}}}
6. Heap Sequencing
void Heapadjust (int *a, int k, int n) {int LHC, rhc;int index = k;while (K < N/2) {LHC = 2 * k + 1;RHC = LHC + 1;if (LH C<n&&a[lhc]>a[index]) {index = LHC;} if (Rhc<n&&a[rhc]>a[index]) {index = RHC;} if (index = = k) {break;} else {//swap (a[k], a[index]); int temp = a[k];a[k] = A[index];a[index] = temp;k = Index;}}} void Heapsort (int *a, int n) {for (int i = N/2-1; I >= 0; i--) {Heapadjust (A, I, n);} for (int i = n-1; I >= 1; i--) {//swap (A[i], a[0]); int temp = A[i];a[i] = a[0];a[0] = temp;heapadjust (A, 0, i);}}
7. Test procedures
int main (void) {Srand (Time (NULL)); const int size = 10000000;//int A[9] = {--------- RT, End;int *a = new Int[size];int *b = new Int[size];int *c = new Int[size];int *temp = new Int[size];for (int i = 0; I & Lt Size i++) {C[i] = b[i] = a[i] = rand ()% 2534541;//cout << A[i] << ""; cout << Endl;start = Clock ();//mergesort (c, 0, size-1,temp);//priority_queue<int> que (c, c + size); heapsor T (c, size),//bubblesort (c, size),//sort (c, C + size),//stable_sort (c, C + size),//qsort (A, size, sizeof (int), CMP); end = C Lock (); cout << "heapsort:" << end-start << "MS" << Endl;start = Clock ();//mergesort (A, 0, size -1, temp);//insertsort (A, size);//bubblesort (A, size); Shellsort (A, size); end = Clock (); cout << "Shellsort:" <& Lt End-start << "MS" << Endl;start = Clock () quickSort (b, 0, size-1); end = Clock (); cout << "QuickSort: "<< End-start <<" MS "<< Endl;retuRN 0;}
In general, fast sorting is much more efficient, and note that instead of using swap functions, especially heap sorting, it may be that too many function calls result in too little efficiency.
Sort Large Totals