标签:
#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