1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
template<typename T> void Quicksort(T*,int);
template<typename T> void Quicksort(T*,int,int);
template<typename T> T Partition(T*,int,int);
template<typename T>
void Quicksort(T a[], int elements){
Quicksort(a,0,elements);
}
template<typename T>
void Quicksort(T a[], int l, int r){
if(l<r){
T pivot = Partition<T>(a,l,r);
Quicksort(a,l,pivot-1);
Quicksort(a,pivot+1,r);
}
}
template<typename T>
T Partition(T a[], int l, int r){
int pivot = a[l];
while(1){
while(a[l]<pivot) ++l;
while(a[r]>pivot) --r;
if(l<r) swap(a[l],a[r]);
else return r;
}
}
int main(){
double array1[]={5.3, 8, 1.5, 3.4};
Quicksort<double>(array1,0,4);
for(int i=0; i<4; i++) cout << array1[i] << endl;
return 0;
}
| |