/* Inline sort */void insertsort (vector<int> &arr) {for (int i = 1;i < Arr.size (), ++i) {for (int j = i;j > 0;--J) {if (Arr[j] < arr[j-1]) {int temp = arr[j]; ARR[J] = arr[j-1]; ARR[J-1] = temp; } else break; }}}/* Bubble sort */void BubbleSort1 (vector<int> &arr) {for (int i = 0; i < arr.size ()-1; i++) {bool Flag=true; for (int j = 0; J < arr.size ()-i-1; j + +) {//1th trip to find out the maximum number if (Arr[j] > arr[j + 1]) { int temp = Arr[j]; ARR[J] = arr[j + 1]; Arr[j + 1] = temp; Flag=false; }} if (flag) return; }}void BubbleSort2 (vector<int> &arr) {for (int i = 0; i < arr.size ()-1; i++) {bool flag=true; for (int j = arr.size ()-1; j > i; j--) {if (Arr[j] < arr[j-1]) {int temp = arr[j]; ArR[J] = arr[j + 1]; Arr[j + 1] = temp; Flag=false; }} if (flag) return; }}/* Quick Sort */void quicksort (vector<int> &arr, int left, int.) {if (left >= right) return; int i = left; int j = right; int key = Arr[left]; The comparison is the first number while (I < J) {while (I < J && Key <= A[j]) { j--; } Arr[i] = Arr[j]; while (I < J && Key >= A[i]) {i++; } Arr[j] = Arr[i]; } Arr[i] = key;/* Returns the median key (arr, left, i-1) after it has been found within a group; Quicksort (arr, i + 1, right); }/* Merge sort */void merge (int& arr[], int reg[], int start, int end) {if (Start >= end) return; int mid = (end + start) >> 1; Divided into two parts int start1 = start, End1 = mid; int Start2 = mid + 1, End2 = end; Then merge merges (arr, Reg, Start1, end1); Merge (arr, Reg, Start2, End2); int k = start; Two sequences are compared, which elements of the sequence are placed in the reg sequence, then position +1 and another sequence of the original position of the element comparison//So repeatedly, you can combine two ordered sequences into an ordered sequence while (Start1 <= end1 && Start2 <= end2) reg[k++] = Arr[start1] < Arr[start2]? Arr[start1++]: arr[start2++]; And then here is the case, if the arr2 sequence has all been put in the Reg sequence and then jumped out of the loop//That means that the arr sequence has a larger element (one or more) is not placed in the Reg sequence, so this step is to put the while (Start1 <= end1) reg[k++] = arr[start1++]; This step is the same as above while (Start2 <= end2) reg[k++] = arr[start2++]; Put the ordered Reg sequence back in the arr sequence for (k = start; k <= end; k++) arr[k] = reg[k];} void MergeSort (int& arr[], const int len) {//Create a sequence of the same length for temporary storage of int reg[len]; Merge (arr, Reg, 0, len-1);} Binary Insert Sort O (NLOG2 (n)) stable int low,high,middle;void binaryinsertsort (int a[], int len) {for (int. i=0;i<len;i++) {in T Temp=a[i]; Low=0;high=i-1; while (Low<=high) {middle= (Low+high)/2; if (Temp<a[middle]) {high=middle-1; } else{Low=middle+1; }} for (int k=i;k>low;k--) {a[k]=a[k-1]; } a[low]=temp; }}/**************************************///Hill the sorting speed is difficult to quantify but the efficiency is very high unstable algorithm void shellsort (int a[], int len) {int gap=len-1 ; Initial increment bool flag=0; while (gap>=1) {if (gap==1) flag=1; for (int i=0;i<len;i++) {if (I+gap<len) {if (A[i]>a[i+gap]) {int temp=a[i]; A[I]=A[I+GAP]; A[i+gap]=temp; }} else break; } gap=gap/3+1; Increment calculation formula if (flag) break; }}//heap sort void heapsort (int arr[],int len) {int i; Initializes the heap, starting with the last parent node for (i = LEN/2-1; I >= 0; i) {heapify (Arr,i,len); }//Remove the largest element from the heap and resize the heap for (i = len-1;i > 0;--i) {int temp = Arr[i]; Arr[i] = arr[0]; ARR[0] = temp; Adjust piles of heapify (arr,0,i); }}//again see adjusting piles of functions void heapify (int arr[], int first, int end) {int father = first; int son = Father* 2 + 1; while (Son < end) {if (son + 1 < end && Arr[son] < arr[son+1]) ++son; If the parent node is greater than the child node, the adjustment is complete if (Arr[father] > Arr[son]) break; else {//otherwise swap the parent node and the child node of the element int temp = Arr[father]; Arr[father] = Arr[son]; Arr[son] = temp; Parent and child nodes become the next place to compare father = Son; son = 2 * father + 1; } }}
Simple implementation of various sorts of C + +