This paper implements eight commonly used sorting algorithms: Insert sort, bubble sort, select Sort, hill sort, quick sort, merge sort, heap sort and lst cardinal order.
First, the algorithm implementation file Sort.h, the code is as follows:
* * Implements eight common sorting algorithms: Insert sort, bubble sort, select sort, Hill sort * and quick sort, merge sort, heap sort and lst cardinal sort * @author gkh178/#include <iostream> tem
Plate<class t> void Swap_value (t &a, t &b) {T temp = A;
A = b;
b = temp;
}//Insert sort: 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 sort: 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 Sort: Time complexity O (n^2) template<class t> void Select_sort (T a[], int n) {for (int i = 0; I &l T 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 Sort: Time complexity between O (n^2) and O (nlgn) 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;
Quick sort: 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 = _PA
Rtition (A, left, right);
_quick_sort (A, left, q-1);
_quick_sort (A, q + 1, right);
} template<class t> int _partition (t a[], int. left, int right) {T pivot = A[left]; while (left < right) {WhilE (Left < right && A[right] >= pivot) {--right;
} A[left] = A[right];
while (left < right && A[left] <= pivot) {++left;
} A[right] = A[left];
} A[left] = pivot;
return to left;
//Merge Sort: 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 = Le
FT + (Right-left)/2;
_merge_sort (A, left, mid);
_merge_sort (A, mid + 1, right);
_merge (A, left, Mid, right);
} template<class t> void _merge (T a[], int left, int mid, int right) {int length = Right-left + 1;
T *newa = new T[length];
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;
while (i <= mid-left) {a[k++] = * (Newa + i);
++i;
while (J <= Right-left) {a[k++] = * (Newa + j);
++j;
} Delete Newa; -//Heap Sort: Time complexity O (NLGN) template<class t> void Heap_sort (T a[], int n) {built_max_heap (A, n);//Establish initial Dagen/
/swap the first and last elements, and adjust the array of the trailing elements after swapping for (int i = n-1 i >= 1;-i) {Swap_value (a[0), a[i]);
Up_adjust (A, I);
Establish a large root heap template<class t> void Built_max_heap (T a[], int n) {up_adjust (A, n) of length n; Template<class t> void Up_adjust (T a[], int n) {//For each element with a child node to traverse the processing, from the back to the root node position for the array of length n (in t i = N/2; I >= 1;
-i) {Adjust_node (A, n, i); The value of the node that adjusts ordinal number i is Template<class t> void Adjust_node (T a[], int n, int i) {//node has left or right child if (2 * i + 1) ; = N)
{//Right child's value is greater than node value, 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, swapping them if (a[2 * i-1] > A[i-1]) {swap_value (a[2 * i-1], a[i-1]);
//Adjust the root node of the node's left and right children Adjust_node (A, N, 2 * i);
Adjust_node (A, N, 2 * i + 1);
//node only left child, for the last node with left and right children else if (2 * i = = N) {//left child's value is greater than node value, exchange them if (A[2 * i-1] > A[i-1])
{Swap_value (a[2 * i-1], a[i-1]); }}//Cardinality the time complexity of sorting is O (distance (n+radix)), distance is the number of digits, n is the number of numbers, radix is cardinality/The method is sorted by the LST method, and the MST method is not included in//where parameter radix is the base Number, typically 10;distance represents the longest number of digits of the array to be sorted; n an array of lengths template<class t> void Lst_radix_sort (T a[], int n, int radix, int dista NCE) {t* Newa = new t[n];//for staging array int* count = new int[radix];//for counting sort, save the number of int div that appears with the current bit value of 0 to Radix-1
IDE = 1; Processed from penultimate to first for (int i = 0; i < distance ++i) {//To row array copy to 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 value of the current processing bit of the array element
(* (count + radixkey)) + +; Each element in count[] saves the number of occurrences of the radixkey bit at that time//calculates the end position of each radixkey in the array, and the position ordinal range is 1-n for (int j = 1; j < Radix; ++j
{* (count + j) = * (Count + j) + * (count + j-1); ///Use the principle of counting sort to achieve a sort, sorted array 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;
}
}
Then 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] &
lt;< "";
} cout << Endl; int main () {for (int i = 1; I <= 8; ++i) {int arr[] = {45, 38, 26, 77, 128, 38, 25, 444, 61, 153, 9
999, 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;
}
Finally, run the result diagram, as follows:
The above is the C + + implementation of the eight commonly used sorting algorithm of all the code, I hope that we have a further understanding of the C + + sorting algorithm.