This is a note;
Quick Sort idea: First say a trip to divide: An array a[0 ... ( LENGTH-1)], take the first element of the array as a pivot V (Vlaue, not subscript), move all elements in the array that are smaller than k to the left of K, and move all elements greater than V to the right of the element.
We need to get an array to divide the position of the pivot v now subscript (this is the boundary to divide next);
An array of length n, on average, requires a log (n) division.
Code implementation:
1 ///Quick Sort2 voidQuick_sort (intA[],intSintt) {//This is the beginning of the algorithm, we see that the input s\t are subscript, subscript and length must be differentiated from the beginning3 if(s<t) {///Then when does this algorithm end? This is a very common problem? ----------"As long as the length of the a[] is greater than 1, which means s<t, we divide it. 4 intK =Partition (a,s,t);/////////A trip to the next dividing boundary, the element on the K is lined up. We can take advantage of this nature and do a lot of meaningful things. , such as finding the smallest K number in array A;5Quick_sort (a,s,k-1);6Quick_sort (a,k+1, T);7 }8 }9 Ten One intPartition (intA[],intSinte) {////This is the partitioning algorithm, while the following is classic. A inttemp = A[s];///Store the A[s], first create a temporary variable to hold the value of the element at the current position. - while(s<D) {///As long as there is more than one element, we run the partitioning algorithm - while(S<e && a[e]>=temp) e--////// from the back of the array, first find the first element less than TMP-- theA[s] =a[e];///--> This element in exchange with A[s] - while(S<e && a[s]<=temp) s++////// from the front of the array to find the first element greater than TMP -A[e] =a[s];///This element and the A[s] Exchange -}/// while +A[s] =temp;///Place the TMP value in the final position, - returns;///Returns the index of the found element +}//Partition
Heap Sort:
Heap sequencing idea:
What big pile? Each node element in a fully binary tree is greater than or equal to its child node value; There is nothing between the children
We first need to build an array of a[] in accordance with the Dagen standard,
(1) Build a heap---"Swap the first and last elements of array A,-----" repair heap
Step (1) We need to iterate over size (a)-1 times in order to get it sorted.
This thought is important because it can build up the heap in O (n) time in the process of sequencing. But after the heap is built, each element of the remaining (n-i) is sorted within log (n-i) time;
Code:
1 ///Heap_sort Algorithm2 voidHeapsort (intA[],intN) {///n is the index and not the length3 for(inti = n/2; i>=0; i--) {///build a big root heap4 Shift (a,i,n);5 for(intp =0;p <=n;p++){6cout<<a[p]<<" ";7}cout<<Endl;8 }9 Ten for(inti = n;i>0; i--){ One inttemp = a[0]; Aa[0] =A[i]; -A[i] =temp; -Shift (A,0, I-1);///build the a[0. I-1)] As a big-root heap the } - } - voidShift (intA[],intIintm) {///m is the index and not the length - ///This algorithm assumes that each element in A[I+1...M] satisfies the definition of a heap, and this algorithm adjusts a[i] to make the sequence a[i. The elements in m] satisfy the nature of the heap + ///requirements are not high, but each time the number of elements of Dagen one more - ///the process of regulating the heap, each time only in a subtree operation, Exchange; + ///so when to change the deadline, that is to say A[i] found the location to find, on the line, our function is the end of the task. A inttemp = A[i];///staging a[i] to temp at if(i==1){ -cout<<"Start"<<Endl; - } - for(intj =2*i+1; j<=m;j=2*i+1){///A[i] The right and left children's subscript is [2i],[2i+1] - if(J<m && a[j]<a[j+1]) J + +;///if the right child of A[i] is present, and the keyword is larger, filter along the right child - ///find the current A[i] the largest child node and A[i] compared to, in ///Dagen Definition: Only the node and the child node comparison, no child nodes between the meaning of the comparison - if(Temp<a[j]) {///this kid's key is bigger. toA[i] = A[j];///put the child's node in the parent's place. +i = j;///Modify the node that is currently being adjusted -}Else{ the Break;///Adjustment Complete * } $}/// forPanax NotoginsengA[i] = temp;///Place the initially adjusted node in the correct position -}
To comb the process of building a heap on one side:
Building the heap is also the process of constantly changing the heap shift, and the shift (int a[],int i,int m) function does the work of A[I...M the array] (all subscripts).
Merge sort:
Merge sort idea:
Code:
1 ///Merger_sort Algorithm2 voidMergearray (int* Arrs,int* Temparr,intLeftintMiddle,intRight ) {3 inti = left, j =Middle;4 intm = middle +1, n =Right ;5 intK =0;6 7 while(i <= j && M <=N) {8 if(Arrs[i] <=Arrs[m])9temparr[k++] = arrs[i++];Ten Else Onetemparr[k++] = arrs[m++]; A } - while(I <=j) -temparr[k++] = arrs[i++]; the while(M <=N) -temparr[k++] = arrs[m++]; - - for(i=0; I < K; i++) +Arrs[left + i] =Temparr[i]; - } + A voidMergeSort (int* Arrs,int* Temparr,intLeftintRight ) { at if(Left <Right ) { - intMiddle = (left + right)/2; - mergesort (Arrs, Temparr, left, middle); -MergeSort (Arrs, Temparr, Middle +1, right); - Mergearray (Arrs, Temparr, left, middle, right); - } in } - to};
Algorithm Basics-Quick sort, heap sort, merge sort