Improved quick sort (average complexity O (NLOGN))

Source: Internet
Author: User

#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))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.