Data Structure-fast sorting by C language (multiple versions)
Quick sorting basically includes the following versions:
I,Two-way scanning of domestic textbooks
II,Unidirectional scan version
Iii. randomization version
4. Division of three numbers
5. Non-recursive Edition
Here are the first two versions (more common)
Version 1:
Bidirectional scan version:
The Code is as follows:
// Quick sorting (version 1) // With Pivot // Yang Xin # include
# Include
# Define MAXN 100int a [MAXN + 1], n; void QuickSort (int left, int right) {int I, j, t, temp; if (left> right) return; temp = a [left]; I = left; j = right; while (I! = J) {while (a [j]> = temp & I <j) {j --;} while (a [I] <= temp & I <j) {I ++;} if (I <j) {t = a [I]; a [I] = a [j]; a [j] = t ;}} a [left] = a [I]; a [I] = temp; QuickSort (left, I-1); QuickSort (I + 1, right);} int main () {printf (=========== Quick Sort version 1 ===========); int I = 0, j, t, count = 0; int T; printf (enter the number of data sorted :); scanf (% d, & T); while (T --) {scanf (% d, & a [I]); I ++;} QuickSort (0, I-1); printf (the sorted data is :); for (j = 0; j <I; j ++) {printf (% d, a [j]);} return 0 ;}
Version 2:
Single scan:
Void quickSort2 (int x [], int l, int r) {if (l> = r) return; int m = l; for (int I = l + l; I <= r; I ++) {if (x [I] <x [l]) {swap2 (x [++ m], x [I]);} swap2 (x [l], x [m]); quickSort2 (x, l, m-1); quickSort2 (x, m + 1, r );} void swap2 (int & a, int & B) {if (a = B) return; // the result of data exchange to the same address is 0a = a ^ B; B = a ^ B; a = a ^ B ;}