Seven basic sorting algorithms (all internally sorted):
Direct Insert Sort
Hill sort
Bubble sort
Simple selection sorting
High-speed sequencing
Heap Sort
Two-way merge sort
Sorting algorithm Stability: After sorting, the relative order between elements with the same key code remains constant, then the ordering method is called stable; otherwise unstable.
Direct Insert Sort:
void Insertsort (inta[],int N) {//index start at1,a[0] is temp one int i,j;for (i=2; i<=n;i++) { if(a[i]<a[I1]){a[0]=aI; a[i]=a[I1];for (j=i-2; a[j]>a[0];j--) { a[j+1]=a[j];}a[j+1]=a[0];} }}
Direct Insert sort is a stable sort, time complexity O (n^2), spatial complexity is O (1)
Hill Sort:
The elements are divided into subsets by increments, and the subsets are continuously sorted by insertion.
void Shellsort (inta[],int N) {//index start at1,a[0] is temp one int d,i,j,k;for (d=n/2;d >=1;d>>=1) {for (i=d+1; i<=n;i++) {//Insertsort if(a[i]<a[I-d]) {a[0]=aI; a[i]=a[I-d];for (j=i-2*d; j>0&&a[0]<a[j];j-=d) { a[j+d]=a[j];}a[j+d]=a[0];} } }}
Suppose a[i]>a[i-d] is always true. The time is O (Nlogn), but in the worst case it is O (n^2).
Space complexity is O (1)
Hill sort is an unstable sort of method
Bubble Sort:
Adjacent elements assume the inverse of the 22 interchange until all positions are determined.
void BubbleSort(int a[],int// index start at 1, a[0] is temp one int i,j,k; for(i=1;i<=n;i++){ for(j=1;j<=n-i;j++){ if(a[j]>a[j+1]) { a[j]=a[j]^a[j+1]; a[j+1]=a[j]^a[j+1]; a[j]=a[j]^a[j+1]; } } }}
This is a stable sorting method, time complexity: O (n^2)
High-speed sequencing:
Select an axis value. Makes the value of the element on the left less than it, the value of the element on the right is greater than it. Repeated appeals process for the resulting partition.
The algorithm is an improvement on the bubbling sort.
intPartion (intA[],intStartintEnd) {intI=start,j=end;intTemp=a[start]; while(I<J) { while(I<j && a[j]>=temp) j--; A[I]=A[J];//I is more while(I<j && a[i]<=temp) i++; A[j]=a[i];//J is more} a[i]=temp;//At end, I=j returni;}voidQsort (intA[],intStartintEnd) {if(Start<end) {intD=partion (A,start,end); Qsort (A,START,D); Qsort (a,d+1, end); }}
High-speed sequencing is not a stable sorting algorithm. On average, the time complexity of Qsort is O (nlogn)
Simple selection Sort:
Thought: The first trip will be sorted record R[I......N] The smallest element and R[i] Exchange
void SelectSort(int a[],int n){ for(int i=1;i<n;i++){ int dex=i; for(int j=i+1;j<=n;j++){ if(a[dex]>a[j]) dex=j; //useindexand find min one } if(dex!=i) { a[dex]=a[dex]^a[i]; a[i]=a[dex]^a[i]; a[dex]=a[dex]^a[i]; } }}
Heap Sort:
The heap is divided into Dagen and small Gan. The parent node is larger or smaller than the left or right child.
The nature of the maintenance heap:
Heap sorting idea: Build the heap first. Build the heap from the bottom up.
The root node is then fetched and output, and the last element is placed on the root node to maintain the heap.
Repeat the above process.
void Sift (inta[],int s,int N) {int i=s,j=2*s; while(j<=n) { //if(J<n &&a[j]>a[j+1]) j=j+1; Small heap get big --and small//if(a[i]<=a[j]) Break; if(J<n &&a[j]<a[j+1]) j=j+1; Big heap Get small --and big if(a[i]>=a[j]) Break; Else{Swap (a[I],a[j]);I=j; J=2*j;}}}void heapsort (inta[],int s,int N) {for (int i=n/2; i>=1;i--) Sift (A,i,n); Build a heap bottom -upShowa, N);for (int i=n; i>1;i--) {Swapa[1],a[i]);Sift (a,1, I-1);}}
The time complexity of heap sequencing is O (NLOGN), which is an unstable sorting algorithm
Two-way merge sort:
The first is the ordering of adjacent elements. Recursively, the sequence of the adjacent subset is sorted, and the last is finished.
Const intn=1e3;intB[n];voidMergeintA[],intSdex,intMdex,intEdex) {inti=sdex,j=mdex+1, K=sdex; while(I<=mdex&&j<=edex) {if(A[i]<a[j]) b[k++]=a[i++];ElseB[k++]=a[j++]; } while(i!=mdex+1) b[k++]=a[i++]; while(j!=edex+1) b[k++]=a[j++]; for(i=sdex;i<=edex;i++) a[i]=b[i];}voidMergeSort (intA[],intSdex,intEdex) {intMdex;if(Sdex<edex) {mdex= (Sdex+edex)/2; MergeSort (A,sdex,mdex); MergeSort (a,mdex+1, Edex); Merge (A,sdex,mdex,edex); }}
Basic sorting algorithm