#include <iostream>using namespace std; #define MAXSIZE 21typedef int sqlist[maxsize]; #define ElementType intvoid Swap (int &a, int &b) {a = A^b; b = a^b; A = A^b; }//*********************************************************elementtype Median3 (ElementType *A, int left, int. right) {int Center = (left + right)/2;if (a[left] > a[Center]) swaps (a[left], a[center]); if (a[left] > a[ Right]) swaps (a[left], a[right]), if (a[Center] < a[right]) swap (a[center], a[right]); return a[right]; /* return pivot */}void Quicksort (elementtype *a, int left, int. right) {if (left>=right) {return;} {Intpivot = Median3 (A, left, right); Inti = left; int j = right; for (; ; {while (i<j && a[i] < Pivot) {++i; }//Note here is >= otherwise when pivot and kinsoku data are equal, the loop while (I<j && a[j] >= Pivot) {--j; } if (I < j) Swap (A[i], a[j]); else break; }swap (A[i], a[right]); QuicksORT (A, left, i-1); Quicksort (A, i+1, right); }}void Quick_sort (ElementType *a,int N)//interface {Quicksort (A, 0, N-1);} void Main () {SqList sq = {49, 38, 65, 97, 76, 13, 27, 49}; Quick_sort (sq,8);//for (int i = 0; i < 8; i++) {cout << sq[i] << ","; } cout << Endl; }
In order to minimize the complexity, there are some border control problems, with the help of teachers, completed the code.
There are shortcomings, also hope to point out.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Improved quick sort (average complexity O (NLOGN))