Quick sort basically has the following version
First, the domestic teaching materials two-way scanning version
Two, one-way scan version
Third, the version of randomization
Four or three-digit segmentation method
Five, non-recursive version
Here are my top two versions (more commonly used)
Version One:
Bidirectional Scan version:
The code is as follows:
Quick Sort (Version one)//pivot//Xin Yang # include <stdio.h> #include <stdlib.h> #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 ("========= fast sort Version One ==========\n"), int i = 0, J, t, Count = 0;int t;printf ("Enter the number of data to sort by: \ n"); scanf ("%d", &T); while (t--) {scanf ("%d", &a[i]); i++;} QuickSort (0, i-1);p rintf ("sorted data is: \ n"), for (j = 0; J < i; J + +) {printf ("%d", A[j]);} return 0;}
Version two:
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;//data Interchange for the same address, which results in 0a=a^b;b=a^b;a=a^b;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure---C language for fast sorting (multiple versions)