Quick Sort C + + implementation

Source: Internet
Author: User

Quick sort #include<iostream> #include <functional> #include <windows.h>using namespace Std;void qksort (  int* arr, int cnt) {function<int (int*, int, int) > Getpivot = [&] (int* arr, int left, int. right)->int{int mid =   (left + right)/2;if (Arr[left] > Arr[mid]) swap (Arr[left], arr[mid]); if (Arr[left] > Arr[right]) swap (Arr[left], arr[right]), if (Arr[mid] > Arr[right]) swap (Arr[mid], arr[right]); swap (Arr[mid], arr[right-1]); return arr[right-1];}; Function<void (int*, int, int) > Insertsort = [&] (int* arr, int begin, int end) {int i = 0, j = 0;for (i = begin + 1; I <= end; i++) {int tmp = arr[i];for (j = i; j > Begin && Arr[j-1] > tmp; j--) arr[j] = arr[j-1];arr[j] = tmp;}; Function<void (int*, int, int) > QK = [&] (int* arr, int left, int. right) {if (left + 9 <= right)//when the array element is greater than or equal to 1 0 times we used a quick sort {int pivot = Getpivot (arr, left, right); int i = left;int j = Right-1;while (1) {while (Arr[++i] < pivot) { }while (Arr[--j] &Gt Pivot) {}if (i < j) Swap (Arr[i], arr[j]); elsebreak;} Swap (Arr[i], arr[right-1]); QK (arr, left, i-1); Qk (arr, i + 1, right);} else//When the array element is less than 10 we use Insert sort Insertsort (arr, left, right);}; QK (arr, 0, cnt-1);}; int main () {int arr[1000];int tmp = -1;for (int i = 0; i <; i++) {if (i% 2) arr[i] = i*tmp;elsearr[i] = i;} for (int i = n; i < i++) {if (i% 2) arr[i] = i*tmp;elsearr[i] = i;} We can do all the above-the-line or part of the fast-sequence part of the insertion sort to test the time, theoretically our number of elements limit is 10, take 10 in some cases is not necessarily the best, but can avoid some harmful special cases {Large_integer Large_ Interger;double Dff;__int64 C1, C2; QueryPerformanceFrequency (&large_interger);d ff = Large_interger. QuadPart; QueryPerformanceCounter (&large_interger); c1 = Large_interger. Quadpart;qksort (arr, 1000); QueryPerformanceCounter (&large_interger); c2 = Large_interger. quadpart;printf ("timed%lf milliseconds \ n", (C2-C1) * 1000/DFF);} for (auto I:arr) cout << i << endl;cin.get (); return 0;}

Quick Sort C + + implementation

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.