QuickSort
Posted purzel
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QuickSort相关的知识,希望对你有一定的参考价值。
#include <bits/stdc++.h> using namespace std; #define MAXSIZE 200000 typedef int KeyType; typedef struct { KeyType key; }RedType; typedef struct { RedType r[MAXSIZE + 1]; int length; }SqList; int Random(int start, int end){ int dis = end - start; return rand() % dis + start; } int Partition(SqList &L, int low, int high) { int pivotkey; L.r[0] = L.r[low]; pivotkey = L.r[low].key; while(low < high) { while(low < high && L.r[high].key >= pivotkey) --high; L.r[low] = L.r[high]; while(low < high && L.r[low].key <= pivotkey) ++low; L.r[high] = L.r[low]; } L.r[low] = L.r[0]; return low; } void QSort(SqList &L, int low, int high) { int pivotloc; if(low < high) { pivotloc = Partition(L, low, high); QSort(L, low, pivotloc - 1); QSort(L, pivotloc + 1, high); } } void QuickSort(SqList &L) { double start_time, finish_time, cord_time; start_time = clock(); QSort(L, 1, L.length); finish_time = clock(); cord_time = (double)(finish_time - start_time) ; printf("QuickSort time=%f ms ", cord_time); } void InPut(SqList &L) { int i; srand((unsigned)time(NULL)); cin >> L.length; for (i = 1; i <= L.length; ++i) { // cin >> L.r[i].key; L.r[i].key = Random(1, 1000000); } } void OutPut(SqList &L) { int i; for (i = 1; i <= L.length; ++i) { cout << L.r[i].key << " "; } } int main() { SqList L; // L.r = new RedType[MAXSIZE +1]; InPut(L); QuickSort(L); OutPut(L); return 0; }
以上是关于QuickSort的主要内容,如果未能解决你的问题,请参考以下文章
poj 2299 Ultra-QuickSort 逆序对模版题
java Heapsort实现和一些基准代码。 mergesort或quicksort的实现速度相当慢..