Sorting Algorithm (1) Fast sorting C ++ implementation

Source: Internet
Author: User

Basic Features of fast sorting time complexity: O (n * lgn) Worst Case: O (n ^ 2) space complexity: Best case: O (lgn), worst case: O (n), average: O (lgn) is unstable. Thanks @ fate for correcting the space complexity of quick sorting. For more information, see. Fast sorting because each recursion occupies a space and returns the intermediate number position, the space complexity of one recursion is O (1 ). The best and average recursive depth is O (lgn), and the corresponding spatial complexity is O (lgn). In the worst case, the recursive depth is O (n ), the space complexity is O (n ). Algorithm QUICKSORT (A, p, r) if p <r then q PARTITION (A, p, r) // key QUICKSORT (A, p, q-1) QUICKSORT (A, q + 1, r) PARTITION (A, p, r) x branch A [r] I branch p-1 for j branch p to r-1 do if A [j] ≤ x then I branch I + 1 exchange A [I] <-> A [j] exchange A [I + 1] <-> A [r] return I + 1 Example of an array to be sorted: 7 3 5 9 8 5 1 10 4 6 clipboard one-stop Sorting Process Analysis: 1 quicksort one-stop sorting process source code class Declaration class BaseSort {public: BaseSort () {} virtual void sort () = 0 ;}; class QuickSort: public BaseSort {public: QuickSort (int Array [], int len): BaseSort () {this-> Array = Array; this-> len = len;} void sort (); private: int partition (int Array [], int start, int end ); void quicksort (int Array [], int start, int end); private: int * Array; int len ;}; void QuickSort: sort () {quicksort (Array, 0, len-1);} void QuickSort: quicksort (int Array [], int start, int end) {if (start <end) {int mid = this-> partition (Array, start, end); if (start <mid-1) quicksort (Array, start, mid-1 ); if (mid + 1 <end) quicksort (Array, mid + 1, end) ;}} int QuickSort: partition (int Array [], int start, int end) {int I, j, x, tmp; x = Array [end]; I = start-1; for (j = start; j <end; j ++) {if (Array [j] <= x) {I ++; tmp = Array [j]; Array [j] = Array [I]; array [I] = tmp ;}} tmp = Array [end]; Array [end] = Array [I + 1]; Array [I + 1] = tmp; if (DEBUG) {printArray (Array, len, "MidResult");} return I + 1;} test: int a [10] =, 4,6}; int len = 10; QuickSort * quicksort = new QuickSort (a, len); quicksort-> sort (); printArray (a, len, "QuickSort ");

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.