Insert sort, bubble sort, select sort, Hill sort, fast sort, merge sort, Heap Sort, and LST base sort-C ++ implementation, Heap Sort lst
The first is the algorithm implementation file Sort. h. The Code is as follows:
<Pre name = "code" class = "java">/** implements eight common sorting algorithms: insert sorting, Bubble sorting, selection sorting, Hill sorting *, fast sorting, Merge Sorting, heap sorting, and LST base sorting * @ author gkh178 */# include <iostream> template <class T> void swap_value (T &, T & B) {T temp = a; a = B; B = temp;} // insert sorting: time complexity o (n ^ 2) template <class T> void insert_sort (T a [], int n) {for (int I = 1; I <n; ++ I) {T temp = a [I]; int j = I-1; while (j> = 0 & a [j]> temp) {a [j + 1] = a [j]; -- j;} a [j + 1] = temp;} // Bubble Sorting: time complexity o (n ^ 2) template <class T> void bubble_sort (T a [], int n) {for (int I = n-1; I> 0; -- I) {for (int j = 0; j <I; ++ j) {if (a [j]> a [j + 1]) {swap_value (a [j], a [j + 1]) ;}}}// select sorting: time complexity o (n ^ 2) template <class T> void select_sort (T a [], int n) {for (int I = 0; I <n-1; ++ I) {T min = a [I]; int index = I; for (int j = I + 1; j <n; ++ j) {if (a [j] <min) {min = a [j]; index = j ;}} a [index] = a [I]; a [I] = min ;}// Hill sorting: the time complexity is between o (n ^ 2) and o (nlgn) between template <class T> void shell_sort (T a [], int n) {for (int gap = n/2; gap> = 1; gap/= 2) {for (int I = gap; I <n; ++ I) {T temp = a [I]; int j = I-gap; while (j> = 0 & a [j]> temp) {a [j + gap] = a [j]; j-= gap ;} a [j + gap] = temp ;}}// fast sorting: time complexity o (nlgn) template <class T> void quick_sort (T a [], int n) {_ quick_sort (a, 0, n-1);} template <class T> void _ quick_sort (T a [], int left, int right) {if (left <right) {int q = _ partition (a, left, right); // The Position of the median obtained by one division _ quick_sort (a, left, q-1); // recursive quick_quick_sort (a, q + 1, right) on the left ); // recursive fast rank on the right} template <class T> int _ partition (T a [], int left, int right) {T break = a [left]; // set the first number to the median while (left <right) {while (left <right & a [right]> = right) {-- right ;} a [left] = a [right]; // from right to left, find a number smaller than the median, place it in the position of the median while (left <right & a [left] <= left) {++ left;} a [right] = a [left]; // search for a number greater than the median from left to right and fill it in the position of the vacancy.} a [left] = left; // enter the median in the left position ;} // Merge Sorting: time complexity o (nlgn) template <class T> void merge_sort (T a [], int n) {_ merge_sort (a, 0, n-1);} template <class T> void _ merge_sort (T a [], int left, int right) {if (left <right) {int mid = left + (right-left)/2; _ merge_sort (a, left, mid); // recursively merges and sorts _ merge_sort (a, mid + 1, right); // recursively merge and sort on the right _ merge (a, left, mid, right ); // merge left and right} template <class T> void _ merge (T a [], int left, int mid, int right) {int length = right-left + 1; T * newA = new T [length]; // store the original array for (int I = 0, j = left; I <= length-1; ++ I, ++ j) {* (newA + I) = a [j];} int I = 0; int j = mid-left + 1; int k = left; for (; I <= mid-left & j <= length-1; ++ k) {if (* (newA + I) <* (newA + j) {a [k] = * (newA + I); ++ I ;} else {a [k] = * (newA + j); ++ j ;}/// enter the excess number on the left or right in the array while (I <= mid-left) {a [k ++] = * (newA + I); ++ I ;}while (j <= right-left) {a [k ++] = * (newA + j); ++ j;} delete [] newA;} // heap sorting: time complexity o (nlgn) template <class T> void heap_sort (T a [], int n) {built_max_heap (a, n); // create an initial large root heap // swap the first and last elements, then, the array excluding the ending element after the switch is adjusted for (int I = n-1; I> = 1; -- I) {swap_value (a [0], a [I]); up_adjust (a, I) ;}// create a large root heap template with a length of n <class T> void built_max_heap (T a [], int n) {up_adjust (a, n) ;}// tune the template <class T> void up_adjust (T a [], int n) on the array whose length is n) {// traverse each element with child nodes, from the back to the root node location for (int I = n/2; I> = 1; -- I) {adjust_node (a, n, I) ;}// adjust the template <class T> void adjust_node (T a [], int n, int I) {// The node has left and right children if (2 * I + 1 <= n) {// the value of the right child is greater than the value of the node, exchange them if (a [2 * I]> a [I-1]) {swap_value (a [2 * I], a [I-1]);} // The value of the left child is greater than the value of the node. if (a [2 * I-1]> a [I-1]) {swap_value (a [2 * I-1], a [I-1]);} // adjust the adjust_node (a, n, 2 * I); adjust_node (a, n, 2 * I + 1);} // The node has only the left child, for the last node with left and right children, else if (2 * I = n) {// The value of the left child is greater than the value of the node, exchange them if (a [2 * I-1]> a [I-1]) {swap_value (a [2 * I-1], a [I-1]) ;}}// the time complexity of the base sorting is o (distance (n + radix), distance is the number of digits, and n is the number of arrays, radix is the base number. // This method uses the LST Method for base sorting. The MST method is not included. // The parameter radix is the base number, which is generally 10; distance indicates the maximum number of digits in the array to be sorted. n is the array length template <class T> void lst_radix_sort (T a [], int n, int radix, int distance) {T * newA = new T [n]; // used to store an array int * count = new int [radix]; // used for counting sorting, the number of int divide = 1 for elements with the current BITs ranging from 0 to radix-1 is saved; // process from the last to the first for (int I = 0; I <distance; ++ I) {// copy the array to be sorted to the newA array for (int j = 0; j <n; ++ j) {* (newA + j) = a [j];} // set the Count array to 0 for (int j = 0; j <radix; ++ j) {* (count + j) = 0 ;}for (int j = 0; j <n; ++ j) {int radixKey = (* (newA + j)/divide) % radix; // get the current bitwise value of the array element (* (count + radixKey) ++ ;} // In this case, each element in count [] stores the number of radixKey bits // calculates the end position of each radixKey in the array. The position sequence number ranges from 1 to n for (int j = 1; j <radix; ++ j) {* (count + j) = * (count + j) + * (count + j-1 );} // use the counting sorting principle to implement one sorting. The sorted array is output to a [] for (int j = n-1; j> = 0; -- j) {int radixKey = (* (newA + j)/divide) % radix; a [* (count + radixKey)-1] = newA [j]; -- (* (count + radixKey);} divide = divide * radix;} delete [] newA; delete [] count ;}
Next, the test file main. cpp. The Code is as follows:
#include "Sort.h"using namespace std;template<class T>void printArray(T a[], int n){for (int i = 0; i < n; ++i){cout << a[i] << " ";}cout << endl;}int main(){for (int i = 1; i <= 8; ++i){int arr[] = { 45, 38, 26, 77, 128, 38, 25, 444, 61, 153, 9999, 1012, 43, 128 };switch (i){case 1:insert_sort(arr, sizeof(arr) / sizeof(arr[0]));break;case 2:bubble_sort(arr, sizeof(arr) / sizeof(arr[0]));break;case 3:select_sort(arr, sizeof(arr) / sizeof(arr[0]));break;case 4:shell_sort(arr, sizeof(arr) / sizeof(arr[0]));break;case 5:quick_sort(arr, sizeof(arr) / sizeof(arr[0]));break;case 6:merge_sort(arr, sizeof(arr) / sizeof(arr[0]));break;case 7:heap_sort(arr, sizeof(arr) / sizeof(arr[0]));break;case 8:lst_radix_sort(arr, sizeof(arr) / sizeof(arr[0]), 10, 4);break;default:break;}printArray(arr, sizeof(arr) / sizeof(arr[0]));}return 0;}
The running result is shown as follows:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.