码迷,mamicode.com
首页 > 其他好文 > 详细

QuickSort

时间:2018-11-29 15:37:47      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:code   art   ini   otl   quick   ace   lock   tput   printf   


#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\n", 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

标签:code   art   ini   otl   quick   ace   lock   tput   printf   

原文地址:https://www.cnblogs.com/purzel/p/10037312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!