Various common sort, bubble sort, choose sort, insert sort, hill sort, heap sort, quick sort, cardinal sort, bucket sort

Source: Internet
Author: User

A variety of common sort

To start looking for a job, the previous learning of a variety of small knowledge to review, the following is a variety of common sort of simple implementation (bubble sort, choose sort, insert sort, hill sort, heap sort, quick sort, base sort, bucket sort), as to the principle is not written out, code is relatively simple, look at the understanding, No more can find a book or Baidu!

#include <iostream>using namespace std;//bubble void bubblesort (int data[], int length) {if (data = = NULL | | length <= 0 return; for (int i = length-1; i > 0; i) {int exchange = 1;for (int j = i-1; J >= 0;--j) {int tmp;if (Data[i] < Data[j]) {tmp = Data[i];d ata[i] = data[j];d ata[j] = tmp;} Exchange = 0;} if (Exchange) return;}} Bubble sort void bubblesort_2 (int data[], int length) {for (int i = 0; i < length-1; ++i) {int exchange = 0;for (int j = Leng Th-1; J > i; --J) {int tmp;if (data[j-1] > Data[j]) {tmp = Data[j];d ata[j] = data[j-1];d ata[j-1] = tmp;} Exchange = 1;} if (!exchange) return;}} Direct insert sort void insertsort (int data[], int length) {if (data = = NULL | | length <= 0) return;for (int i = 1; i < length; + +) i) {int tmp = Data[i];int j = i-1;while (J >= 0 && data[j] > tmp) {data[j + 1] = data[j];--j;} Data[j + 1] = tmp;}} Directly select sort void selectsort (int data[], int length) {for (int i = 0; i < length-1; ++i) {int k = i;for (int j = i + 1; J &lt ; Length ++J){if (Data[j] < data[k]) k = j;} if (k! = i) {int tmp = Data[i];d ata[i] = data[k];d ata[k] = tmp;}}} Quick sort void QuickSort (int data[], int start, int last) {if (data = = NULL | | Start > last) return;int i = start, j = last;if (Start < last) {int tmp = Data[start];while (i! = j) {while (J > I && data[j] > tmp)--j;data[i] = Data[j];while (i < J &amp ;& Data[i] < tmp) ++I;DATA[J] = Data[i];} Data[i] = tmp; QuickSort (data, start, i-1); QuickSort (data, i + 1, last);}} Hill sort void Shellsort (int data[], int length) {int gap = Length/2;while (Gap > 0) {for (int i = gap; i < length; ++i) {int tmp = Data[i];int j = i-gap;while (J >= 0 && tmp < DATA[J]) {data[j + gap] = Data[j];j = J-gap;} Data[j + gap] = tmp;//j = J-gap;} gap = GAP/2;}} void sift (int data[], int low, int. high) {//If the subscript for the first element is 0, the subscript for the two child nodes is: 2 * 0 = 0, 2 * 0 + 1 = 1//not (coincident with the parent node), which also precedes the subscript of the first element to be 1 reasons for starting. int i = low, J = 2 * i;int tmp = Data[i];while (J <= High) {if (J < l && Data[j] &lT Data[j + 1]) ++j;if (Data[j] > tmp) {data[i] = Data[j];i = J;j *= 2;} Elsebreak;} Data[i] = tmp;}  Heap sort void heapsort (int data[], int length) {for (int i = LENGTH/2; i > 0; i.) SIFT (data, I, length); for (int j = length ; J > 1; --J) {int tmp = data[1];d ata[1] = data[j];d ata[j] = tmp;sift (data, 1, j-1);}} Merge sort void merge (int data[], int low, int. mid, int high) {//one time merges//combines adjacent two ordered tables [low, Mid],[mid + 1, high] into a table if (data = = N ULL | | Low < 0 | | Low > Mid | | Mid > High) return;int i = low, J = mid + 1;//int *data_tmp = (int *) malloc (sizeof (int) * (high-low + 1)); int *data_tm p = new Int[high-low + 1];int k = 0;while (i <= mid && J <= High) {if (Data[i] <= data[j]) {Data_tmp[k] = Data[i];++i;++k;} else {data_tmp[k] = data[j];++k;++j;}} while (I <= mid) {data_tmp[k] = data[i];++i;++k;} while (J <= High) {data_tmp[k] = data[j];++j;++k;} for (int k = 0, i = low; I <= high; ++i, ++k) data[i] = data_tmp[k];} void Mergepass (int data[], int extent, int length) {//merge two longExtent ordered table if (data = = NULL | | Extent <= 0 | extent > Length) return;int i;for (i = 0; i + 2 * Extent < length; I + = 2 * extent) Merge (data, I, i + extent-1, i + 2 * extent-1);//There may be some single non-paired elements if (i + extent-1 < length)// After the element remains small with a extent length, it is not necessary to merge merge (data, I, i + extent-1, length-1);} void mergesort (int data[], int length) {if (data = = NULL | | length <= 0) return;for (int extent = 1; extent < length; ex Tent *= 2) mergepass (data, extent, length);} void Main () {int data[] = {0,9,8,7,1,5,6,3,4,2,45,26,789,12,79,7832,4565,413,46,455,78,58};//bubblesort_2 (data,22)   ;//insertsort (data,22);//insertsort (data,22);//quicksort (data, 0, 21);//shellsort (data,22);//heapsort (data,); The first element is not counted in MergeSort (data,22), for (int i = 0; i <; ++i) cout << data[i] << ""; cout << Endl;}

As for the Cardinal sort and the bucket sort, which was written previously so here is not repeated, in

http://blog.csdn.net/xwchao2014/article/details/44804087


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

Various common sort, bubble sort, choose sort, insert sort, hill sort, heap sort, quick sort, cardinal sort, bucket sort

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.