码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2015-01-03 16:00:24      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

typedef struct{
    int key;
}Data;

typedef struct  {
    Data *data;
    int length;
}Sqlist;

void input(Sqlist &L){
    int n;
    cout<<"please enter a number"<<endl;
    cin>>n;
    L.length=n;
    L.data=(Data *)malloc(L.length+1);
    for(int i=1;i<=n;i++){
        cout<<i<<" number :  ";
        cin>>L.data[i].key;
    }
}

int Pation(Sqlist &L,int low,int high){
    L.data[0]=L.data[low];
    int pivotkey=L.data[low].key;
    while(low<high){
        while(low<high&&L.data[high].key>=pivotkey) --high;
        L.data[low]=L.data[high];
        while(low<high&&L.data[low].key<=pivotkey) ++low;
        L.data[high]=L.data[low];
    }
    L.data[low]=L.data[0];
    return low;
}

void Qsort(Sqlist &L,int low,int high){
    if(low>high) return ;
    if(low<high){
        int pivoloc=Pation(L,low,high);
        cout<<"pivoloc "<<pivoloc<<endl;
        Qsort(L,low,pivoloc-1);
        Qsort(L,pivoloc+1,high);
    }
}//Qsort

void print(Sqlist &L){
     for(int i=1;i<=L.length;i++){
         cout<<i<<" "<<L.data[i].key<<endl;
     }
 }
int main(){
    Sqlist L;
    input(L);
    print(L);
    Qsort(L,1,L.length);
    print(L);
}

快速排序

标签:

原文地址:http://blog.csdn.net/u013076044/article/details/42362389

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