1. Quick Sort Description:
It is divided into three parts: decomposition, settlement and merger based on the divide-and-conquer model;
1) Decomposition:
A[p the array: R] divided into two sub-arrays a[p: Q-1] and A[Q+1..R], yes a[p. Q-1] Each element is less than or equal to a (q)
2) Resolve:
Quick ordering by recursive invocation, a[p of the subarray. Q-1] and A[Q+1..R] Sort
3) Merger:
Merge two ordered arrays, a[p. R
1#include <stdio.h>2#include <stdlib.h>3 4 #defineMaxSize 85 6 intPartition (intA[],intPintR) {7 intx = A[r];//the last element in a[] as the principal, the Datum8 inti = P-1;9 intJ;Ten inttmp; One A //through for () {...} Divide the array, because the last element is the principal, so J < (R-1) - for(j = P; J < R; J + +){ - if(A[j] <= x) {//The element is smaller than the primary, placed in front, and swapped two elements for position, I <= J thei = i +1; - -TMP =A[i]; -A[i] =A[j]; +A[J] =tmp; - //printf ("\nswaped:a[%d]:%d, a[%d]:%d\n", I, J, A[i], a[j]); + } A } at //Place The main element between less than and greater than two parts -TMP = A[i +1]; -A[i +1] =A[r]; -A[R] =tmp; - - return(i +1); in } - to voidQuickSort (intA[],intPintR) { + if(P <R) { - intQ; the *Q =Partition (A, p, R); $QuickSort (A, p, Q-1);Panax NotoginsengQuickSort (A, q +1, R); - } the } + A intMain () { the intA[maxsize] = {2,1,1,1,3,5,6,4}; + inti; - $printf"a[]...\n"); $ for(i =0; i < MaxSize; i++){ -printf"%d", A[i]); - } the -QuickSort (A,0, (MaxSize-1));Wuyi theprintf"\nsorted a[]...\n"); - for(i =0; i < MaxSize; i++){ Wuprintf"%d", A[i]); - } Aboutprintf"\ n"); $ -System"Pause"); - return 0; -}
Introduction to Algorithms--seventh chapter, Quick Sort