Insert sort, bubble sort, select Sort, hill sort, quick sort, merge sort, heap sort, and LST cardinality sort--c++ implementation

Source: Internet
Author: User

The first is the algorithm implementation file Sort.h, the code is as follows:

/** implements eight commonly used sorting algorithms: Insert sort, bubble sort, select sort, Hill sort * and quick sort, merge sort, heap sort, and lst cardinality sort * @author gkh178*/#include <iostream>template <class t>void swap_value (t &a, t &b) {T temp = A;a = B;b = temp;}  Insertion 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 < n-1; ++i) {T min = a[i];in T 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 = _partition (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 &L T Right && A[right] >= pivot) {--right;} A[left] = A[right];while (Left < right && A[left] <= pivot) {++left;} A[right] = A[left];} A[left] = Pivot;return 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 ' < right ' {int mid = left + (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 Ordering: Time complexity O (NLGN) template<class t>void heap_sort (T a[], int n) {built_max_heap (A, n);//Establish initial Dagen//swap-first element, and an array of excluded tail elements after swapping for (int i = n-1; I >= 1; i.) {Swap_value (a[0], a[i]); Up_adjust (A, I);}} Build a large root heap of length n template<class t>void built_max_heap (T a[], int n) {up_adjust (A, n);} One-time adjustment of an array of length n template<class t>void up_adjust (T a[], int n) {//For each element with a child node traversal processing, from the back to the root node position for (int i = N/2; I >= 1; -i) {Adjust_node (A, n, i);}}//Adjust the value of the node ordinal to I template<class t>void Adjust_node (T a[], int n, int i) {//node has left and right child if (2 * i + 1 <= N) {//the child's value is greater than node Values, 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, exchange them 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) of the node's left and right child's root node, Adjust_node (A, N, 2 * i + 1);}  Node only left child, for the last one has left and right child's node else if (2 * i = = N) {//left child value 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 radix sort is O (distance (n+radix)), distance is the number of Bits, n is the number of arrays, radix is radix//This method uses the LST method to sort the cardinality, the MST method is not included in//where the parameter radix is the base, typically 10 ; Distance represents the longest number of digits of the array to be sorted; n the length of the arrays Template<class t>void lst_radix_sort (T a[], int n, int radix, int distance) {t* NE   WA = new t[n];//used for staging an array int* count = new int[radix];//is used for the count sort, which holds the number of elements of the current bit with a value of 0 to radix-1, int divide = 1;//from the penultimate to the first bit for (int i = 0; i < distance; ++i) {//rows array copied to Newa array for (int j = 0; J < N; ++j) {* (Newa + j) = A[j];} Place the count array at 0 for (int j = 0; J < Radix; ++j) {* (count + j) = 0;} for (int j = 0; J < N; ++j) {int radixkey = (* (Newa + j)/divide)% radix;//Gets the value of the current processing bit of the array element (* (count + radixkey) + +;} At this point, each element in count[] holds the number of occurrences of the radixkey bit//calculates the end position of each radixkey in the array, 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 sorting to achieve a sort, sorted array output to a[] for (int j = n-1; J >= 0;--j) {int radixkey = (* (Newa + j)/Divide)% radix;a[* (coun T + Radixkey)-1] = newa[j];--(* (count + Radixkey));} divide = divide * radix;}}

Then 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) {C Out << 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 ]), 4; break;default:break;} PrintArray (arr, sizeof (arr)/sizeof (arr[0]));} return 0;}

Finally, run the result diagram, as follows:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Insert sort, bubble sort, select Sort, hill sort, quick sort, merge sort, heap sort, and LST cardinality sort--c++ implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.