#pragma once#include<iostream> #include <assert.h>using namespace std;// Direct ordering: Refers to setting 2 subscript/pointer. Then the comparison starts from subscript 1,//ascending: If the previous subscript/pointer is greater than the current comparison value. The array is moved back. Until the current sequence value is satisfied. The current comparison value is then eventually replaced. PS: There is always a pointer traversal comparison array (K,arry[i])//Time complexity of: 0 (n^2), spatial complexity 0 (1) void insertsort (Int* arry,int len) {assert ( Arry); for (Int i = 1; i < len;++i) {int j = i-1;int k = arry[i];while (j > -1 && arry[j] > k) {arry[j + 1] = arry[j];--j;} arry[j+1] = k;}} Hill sort: Increase the gap value on the basis of direct sorting,//Use gap value, compare the position of corresponding (LEN/GAP) * i. Each position represents a group//and then increments the value of I to len-gap in the same position that I have compared each corresponding grouping so that the current sequence is nearly orderly. We then narrowed the gap value to close to 2, proving that we also need to sort the groups. 0 (n^2), 0 (1), Void shellsort (Int *arry,int len) {assert (Arry); int gap = len;while (gap > 1) {Gap = gap/3 + 1;for (int i = 0; i < len - gap;+ +i) {Int j = i;int k = arry[i+gap];while (j > -1 && arry[j] > k) {arry[ J + gap] = arry[j];j -= gap;} arry[j+gap] = k;}}} Select sort: In fact, this sort of thinking is relatively simple, we only need to iterate through the array//to get the minimum/maximum (or 2), and then drop the maximum value of the minimum value to the left of the array and the right side and then narrow the range. And then it's worth noting that. When we reach the current maximum minimum, we need to be aware that the minimum value of the maximum value currently selected will not be exchanged between Max and Min after one exchange. 0 (n^2), 0 (1) void selectsort (Int *arry, int len) {assert (Arry); for (int i = 0 , j = len-1;i < j;++i,--J) {int min = i;int max = j; for (int k = i;k <= j;++k) {if (Arry[k] < arry[min]) {min = k;} if (Arry[k] > arry[max]) {max = k;} if (min != i) {int temp = arry[min];arry[min] = arry[i];arry[i] = Temp;if (Max == i) max = min;} if (max!= j) {int temp = arry[max];arry[max] = arry[j];arry[j] = temp;}}}} Bubble sort: Bubble sort is relatively simple, not much to say. It is important to note that we can use a tag value to determine if we need to do this one time bubble//If we need to bubble, our tag value will set the bit switch. Then we can reduce the number of times we need to sort. 0 (n^2), 0 (1) void bubblesort (Int *arry,int len) {assert (Arry); for (int i = 0;i < len -1;++i) {for (int j = len - 1;j >= i;--j) {if (Arry [J] < arry[j-1]) {Int temp = arry[j];arry[j] = arry[j - 1];arry [J - 1] = temp;}}} Quick sort: The approximate idea of our quick sort is to select 2 subscripts/pointers at one end, and then we use the pointer selection method//To arrange the large and small sets of numbers. The key value that we selected is built as a critical value, greater than the left side, and less than the right side. And then it narrows it down.//the way to improve efficiency is that we can choose to use Insert sorting when the value of the current sequence is less than a certain number. This can effectively improve the efficiency of our sequencing. The previous one only applies to arrays. 0 (Nlogn), 0 (Logn) int _quicksort (int* arry,int left,int right) {assert (Arry), if (left >= right) return 0;int tmp = arry[right];int index = right;-- Right;while (Left <right) {while (left < right && arry[left] <= tmp) ++left;while (left < right && arry[right] >= tmp) ++right;if (left < right) {swap (arry[left],arry[right]);}} if (Tmp <= arry[right]) {swap (Arry[right],arry[index]);} Return right;} Void quicksort (int *arry,int left,int right) {assert (Arry), if (left < right) { Int mid = _quicksort (Arry,left,right); QuickSort (Arry,mid+1,right); QuickSort (arry,left,mid-1);}} Quick sort: Before and after the pointer, we choose a tightly followed by the 2 pointers, the principle is the same as the previous one, the knowledge of the large number of decimal places,//Such a method can be used in the pointer, of course, key is important to choose,//The worst case is to select the order of the maximum number/minimum number of arrays There will be a worst case scenario. The choice of 3-digit method can effectively avoid the occurrence of this condition. Void swap (int* arry,int left,int right) {Int tmp;tmp = arry[left];arry[left] = arry[right];arry[right] = tmp;} Void quicksort_on (int* arry,int left,int right) {int i,last;if (left >= right) Return ;swap (Arry,left, (left+ (Right-left)/2); Last = left;for (i = left +1; i <= right;++i) {if (Arry[i] < arry[left]) swap (arry,++last,i);} Swap (arry,left,last); quicksort_on (arry,left,last - 1) ; QUICKSORT_ON (arry,last +1,right);} Merge sort: Using the branches of the tree and then using the integration of the interval, to achieve the completion of sorting. Each time we determine an interval (N/2), and then we continue to split the 2-point. In the 2 intervals, we compare the headers of each interval, and then we sort them into the array we need to save. Finally, we can get a relative sort sequence by constantly splitting and merging the duplicates. 0 (Nlogn), 0 (N) void merge (int* arry,int* dest,int begin1,int end1,int begin2, int end2) {Int index = begin1;while (begin1 <= end1 && Begin2 <= end2) {if (arry[begin1] < arry[begin2]) {dest[index++] = arry[ begin1++];} else{dest[index++] = arry[begin2++];}} if (BEGIN1&NBSP;<=&NBSP;END1) {while (BEGIN1&NBSP;<=&NBSP;END1) {dest[index++] = arry[begin1++];}} Else{while (Begin2 <= end2) dest[index++] = arry[begin2++];} Void _merge (int* arry,int* dest, Int left,int right) {//because it is left-closed and right-open. int mid = left+ ((right - left) &NBSP;/2); if (left < right -1) {//int mid = left+ ((right - left) >>1); _merge (Arry,dest,left,mid); _Merge (Arry,dest,mid +1,right); Merge (arry,dest,left,mid,mid+1,right);//memcpy (arry+left,dest+left,sizeof (int) * (right - left +1));} Merge (arry,dest,left,mid,mid+1,right); memcpy (arry+left,dest+left,sizeof (int) * (right - left +1));} Void mergesort (int *arry,size_t size) {Int* dest = new int[size];_merge (Arry , dest,0,size-1);//memcpy (arry,dest,sizeof (int) * (one));d elete[] dest;}
Summarize:
The time complexity of Nlogn has a quick sort, merge sort, heap sort. One of the worst cases of quick sorting is n^2, the rest are n^2, and the hill sort is between n-n^2.
For stability, bubble sort, insert sort, merge sort is stable, other sorts in different cases the stability will be different.
For spatial complexity, the spatial complexity of fast sorting is 0 (LOGN), and the merge sort is 0 (n)
The following is a sort of cardinality and count that can be used only within a qualifying condition:
Count Sort: Time complexity: O (N), Space complexity O (maximum number-minimum number)
Radix sort: Time complexity: O (n number of digits), Space assist O
Cardinal Sort: Use the hash bucket principle to sort data, choose from low to high or from high to low//use sparse matrix compressed storage for data positioning Int getdigit (int* arr, size_t Size) { int maxdigit = 1; int maxnum = 10; for (Int i = 0; i < size; ++i) { if (arr[i] >= Maxnum) { int count = maxDigit; while (Maxnum <= arr[i]) { maxNum *= 10; ++count; } maxDigit = count; } } return maxdigit;} Void lsdsort (int* arr, size_t size)//From the low Start row msd from the high Start row { int counts[10] = { 0 };//number of data int startcounts[10] = { 0 }; int *bucket = new int[size]; int digit = 1;//said to take you int maxDigit = first Getdigit (arr, size); int divider = 1;//Divisor while (digit++ <= maxdigit) { memset (counts, 0, sizeof (int) *&NBSP;10); memset (startcounts, 0, sizeof (int) * &NBSP;10); for (int i = 0; i < size; ++i) counts[(arr[i]/ Divider)% 10]++; for (int i = 1; I < 10; ++i) startcounts[i] = startcounts[i - 1] + counts[i - 1]; for (int i = 0; i < size; ++i) { bucket[startcounts[(Arr[i] / divider) % 10]++] = arr[i]; } divider *= 10; memcpy (arr, bucket, sizeof (int) *size); } memcpy (arr, bucket, sizeof (int) *size); delete[] bucket;} //count Sort Void countsort (int *arr, size_t size,int len) { int * bitmap = new int[len]; memset (bitmap, 0, sizeof (int) * Len); for (int i = 0; i < size; ++i) { int index = arr[i] > >5;//divided by 32 int count = arr[i] % 32;. bitMap[index] |= (1 << count); } int index = 0; for (int i = 0; i < len; ++i) { int count = 0; while (count <32&&index<size ) { if (bitmap[i] & (1 << count)) arr[index++] = i * 32 + count; ++count; } } delete[] bitmap;}
These two sorts of environments can only be used under certain conditions, so they are not included in the conventional sequencing technique, but the efficiency is very impressive.
This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1766133
Summary of "C + +" sorting