1. Quick Sort 1) algorithm Overview
Quick sorting is a sort algorithm developed by C. A. R. Hoare. The basic idea is to separate the records to be sorted into two separate parts by one sort. the keywords of one part of the records are smaller than those of the other, then, the two records can be sorted separately to achieve the whole sequence order.
2) Algorithm Description
The quick sorting method divides a string (list) into two sub-series (sub-lists ).
Steps:
1. Pick out an element from the series, which is called a benchmark ),
2. Re-sort the series. All elements are placed before the benchmark values smaller than the benchmark values, and all elements are placed behind the benchmark values larger than the benchmark values (the same number can reach either side ). After the partition exits, the benchmark is in the middle of the series. This is called a partition operation.
3. recursively sort the subseries smaller than the reference value element and the subseries greater than the reference value element.
Worst time complexity: O (N ^ 2)
Optimal Time Complexity: O (n log n)
Average time complexity: O (n log n)
Worst space complexity: different implementation methods
3) algorithm code
Quick sorting has many versions, but the key is the idea of division.
Void quicksort (int l [], int L, int R) {int I, j, struct; If (L> = r) return; // STD :: swap (L [L], L [(L + r)/2]); // use the intermediate number as the reference number. // int P = L + rand () % (R-l + 1); // rand_partition () // STD: swap (L [L], L [p]); I = L, j = r, sums = L [l]; while (I <j) {While (I <J & L [J]> = sums) // find the first number smaller than the key from the right to the left --; if (I <j) L [I ++] = L [J]; while (I <J & L [I] <= distinct) // find the first number I ++ greater than the key from left to right; if (I <j) L [j --] = L [I];} l [I] = sort; quicksort (l, l, I-1); quicksort (L, I + 1, R );}
void qsort1(int L[], int l, int r){if (l >= r)return;int i = l, j;for (j = l + 1; j <= r; j++) /* invariant: L[l+1..i] < L[l] && L[i+1..j-1] >= L[l]*/if (L[j] < L[l]) //<std::swap(L[++i], L[j]);std::swap(L[l], L[i]);/* L[l..i-1] < L[i] <= L[i+1..r]*/qsort1(L, l, i - 1);qsort1(L, i + 1, r);}
void qsort3(int L[], int l, int r){if (l >= r)return;int pivot = L[l];int i = l, j = r + 1;while(i <= j){do {i++;} while (i <= r && L[i] < pivot);do {j--;} while (L[j] > pivot);if (i > j)break;std::swap(L[i], L[j]);}std::swap(L[l], L[j]);qsort3(L, l, j - 1);qsort3(L, j + 1, r);}
void qsort4(int L[], int l, int r){if (l >= r)return;int pivot = L[l];int i = l - 1, j = r + 1;while(i <= j){do {i++;} while (/*i <= r &&*/ L[i] < pivot);do {j--;} while (L[j] > pivot);if (i > j)break;std::swap(L[i], L[j]);}//print(L, l, r);putchar('\n');std::swap(L[l], L[j]);qsort3(L, l, j - 1);qsort3(L, j + 1, r);}
4) Calculate the K-small number or the minimum first k Number of the array by using the fast partitioning idea.
(1) the k-th small number
// After a division, the number on the left of the principal component is smaller than the principal component, and the number on the right of the principal component is greater than or equal to the principal component int rand_partition (int A [], int L, int R) {int I, j, clerk; int P = L + rand () % (R-l + 1); STD: swap (A [L], A [p]); substring = A [L], I = L, j = r; while (I <j) {While (I <J & A [J]> = numbers) // find the first number less than limit from the right to the left. j --; if (I <j) A [I ++] = A [J]; while (I <J & A [I] <distinct) // from left to right find the first number I ++ greater than or equal to the limit; if (I <j) A [j --] = A [I];} A [I] = random; return I;} int rand_select (int A [], int L, int R, int I) // ith big in a [L ,..., r] {int branch tloc, K; If (L = r) return a [l]; branch tloc = rand_partition (A, L, R); k = branch tloc-L + 1; if (I = k) return a [else tloc]; else if (I <k) return rand_select (A, L, pivotloc-1, I); else return rand_select (, required tloc + 1, R, I-k );}
(2) The minimum number of first K can be used to create a big top heap solution containing k elements. You can also use the Division idea. The Code is as follows:
void GetleastNumber(int input[], int n, int output[], int k){if (n <= 0 || k <= 0 || k > n)return;int l = 0, r = n - 1;int pivotloc = Partition(input, l, r);//l..|....|.....|...r// p < k-1 <= pwhile (pivotloc != k - 1){if (pivotloc < k - 1) pivotloc = Partition(input, pivotloc + 1, r);else pivotloc = Partition(input, l, pivotloc - 1);}memcpy(output, input, k * sizeof(int));}
Ii. Merge Sorting 1) algorithm Overview
Merge Sorting is an effective Sorting Algorithm Based on merge operations. This algorithm is a very typical application of divide and conquer. Merge Sorting is a stable sorting method.
Merges ordered subsequences to obtain a fully ordered sequence. That is, first orders each subsequence, and then orders the subsequence segments. If two ordered tables are merged into an ordered table, it is called a 2-way merge.
2) Algorithm Description
The specific Merge Sorting Algorithm is described as follows (recursive version ):
1. Divide: divides an input sequence whose length is N into two subsequences whose length is n/2.
2. Conquer: sort the two subsequences by merging.
3. Combine: combine two sorted subsequences into a final sorting sequence.
The efficiency of merging and sorting is relatively high. If the length of a series is set to N, splitting the series into small series requires a total of n logn steps. Each step is a process of merging ordered series, the time complexity can be recorded as O (N), so a total of O (N * logn ). Because Merge Sorting is performed on adjacent data each time, Merge Sorting is performed in several sorting methods (fast sorting, Merge Sorting, Hill sorting, and heap sorting) of O (N * logn) it is also highly efficient.
3) The algorithm code can be written in a centralized way by sorting the code. The following describes how to: (1) allocate a temporary array space to avoid frequent memory application in the function.
// Put two ordered arrays A [first ,..., mid], a [Mid + 1 ,..., last] is merged into an ordered void mergetwosortedarray (int A [], int first, int mid, int last, int temp []) {int I = first, j = Mid + 1; int K = 0; while (I <= Mid & J <= last) if (a [I] <A [J]) temp [k ++] = A [I ++]; elsetemp [k ++] = A [J ++]; while (I <= mid) temp [k ++] = A [I ++]; while (j <= last) temp [k ++] = A [J ++]; // for (I = 0; I <K; I ++) // A [first + I] = temp [I]; memcpy (a + first, temp, sizeof (INT) * (last-first + 1);} // recursively calls a [first ,..., last] the elements in the interval are sorted in binary order and then merged into void mergesortr (int A [], int first, int last, int temp []) {If (first <last) {int mid = (first + last)/2; // divide a [first, last] into a [first ,..., mid] And a [Mid + 1 ,..., last] mergesortr (A, first, mid, temp); // left order: recursively convert a [first ,..., mid] is merged into ordered temp [first ,..., mid] mergesortr (A, Mid + 1, last, temp); // right order: recursively convert a [Mid + 1 ,..., last] is merged into ordered temp [Mid + 1 ,..., last] mergetwosortedarray (A, first, mid, last, temp); // after merging, all are ordered: The ordered temp [first ,..., mid] and ordered temp [Mid + 1 ,..., last] merged to a [first, last]} void mergesort (int A [], int N) {int * temp = new int [N]; // temporary array if (temp = NULL) return; memset (temp, 0, sizeof (INT) * n); mergesortr (A, 0, n-1, temp ); delete [] temp ;}(2) If a temporary array space is allocated within the Merge function each time, the Merge function can be written in several ways:
// First copy the left and right ordered sequence to the temporary array and then directly merge it into the original array void mergetwosortedarray1 (int A [], int first, int mid, int last) {int I, j; int n1 = mid-first + 1; int n2 = last-mid; int * l = new int [N1]; // temporary array int * r = new int [n2]; // for (I = 0; I <N1; I ++) // left ordered sequence // L [I] = A [first + I]; // For (j = 0; j <N2; j ++) // R [J] = A [Mid + 1 + J]; // right ordered sequence for (I = first; I <= mid; I ++) L [I-first] = A [I]; for (j = Mid + 1; j <= last; j ++) R [J-mid-1] = A [J]; I = 0, j = 0; int K = first; while (I <N1 & J <N2) {If (L [I] <R [J]) A [k ++] = L [I ++]; elsea [k ++] = R [J ++];} while (I <N1) A [k ++] = L [I ++]; while (j <N2) A [k ++] = R [J ++]; Delete [] l; Delete [] r ;}
// First copy the left and right ordered sequence to the temporary array and then directly merge it into the original array (use the Sentinel) void mergetwosortedarray2 (int A [], int first, int mid, int last) {int I, j; int n1 = mid-first + 1; int n2 = last-mid; int * l = new int [N1 + 1]; // temporary array, int * r = new int [n2 + 1]; // for (I = 0; I <N1; I ++) // left ordered sequence // L [I] = A [first + I]; // For (j = 0; j <N2; j ++) // R [J] = A [Mid + 1 + J]; // right ordered sequence // L [I] = int_max, R [J] = int_max; // Add the sentry element for (I = first; I <= mid; I ++) L [I-first] = A [I] at the end; for (j = Mid + 1; j <= last; j ++) R [J-mid-1] = A [J]; L [I-first] = int_max, R [J-mid-1] = int_max; I = 0, j = 0; For (int K = first; k <= last; k ++) {If (L [I] <R [J]) A [k] = L [I ++]; elsea [k] = R [J ++];} delete [] l; Delete [] r ;}
void MergeSortR1(int a[], int first, int last){if (first < last){int mid = (first + last) / 2;MergeSortR1(a, first, mid);MergeSortR1(a, mid + 1, last);MergeTwoSortedArray3(a, first, mid, last);}}void MergeSort1(int a[], int n){MergeSortR1(a, 0, n - 1);}4) Reverse Order
The thought of merging and sorting can be used to find Reverse Order Number pairs.
Int Merge (int A [], int first, int mid, int last, int temp []) {int inversion = 0; int I = first, j = Mid + 1, k = 0; while (I <= Mid & J <= last) {if (a [I] <= A [J]) {temp [k ++] = A [I ++];} else // A [I]> A [J], while a [first ,.., I ,.., mid] is incremental and ordered, so a [I ~ Mid] And a [J] constitute a reverse order, a total of mid-I + 1 {temp [k ++] = A [J ++]; inversion + = mid-I + 1 ;}while (I <= mid) temp [k ++] = A [I ++]; while (j <= last) temp [k ++] = A [J ++]; memcpy (a + first, temp, sizeof (INT) * (last-first + 1); Return inversion ;} int mergeinversionr (int A [], int first, int last, int temp []) {int inversion = 0; If (first <last) {int mid = (first + last)> 1; inversion + = mergeinversionr (A, first, mid, temp); // find the left half Number of reverse direction pairs of segments inversion + = mergeinversionr (A, Mid + 1, last, temp); // find the number of reverse direction pairs of the right segment inversion + = Merge (A, first, mid, last, temp); // after finding the reverse order of the left and right half segments, find the reverse order of the next two segments, and then find the reverse order of the two segments. The smallest reverse segment has only one element.} Return inversion;} int mergeinversion (int A [], int N) {int inversion = 0; int * temp = new int [N]; memset (temp, 0, sizeof (INT) * n); inversion = mergeinversionr (A, 0, n-1, temp); Delete [] temp; return inversion ;}
Iii. Heap sorting
See use heap to implement heap sorting & priority queue.