模版快速排序

来源:互联网 发布:oracle sql if 子查询 编辑:程序博客网 时间:2024/05/16 09:04
#include <iostream>using namespace std;template <typename T>void swap(T * a,T * b){ T temp=*a; *a=*b; *b=temp;}template <typename T>int parition(T * A,int p,int r){  int i = p-1;  int key = A[r];  for (int j = p; j <= r-1; ++j)    {      if (A[j]<=key){  ++i;  swap(A[i],A[j]);}    }  swap(A[i+1],A[r]);    return i+1;}template <typename T>void quicksort(T * A,int p,int r){    if (p < r)    {      int q = parition(A,p,r);      quicksort(A,p,q-1);      quicksort(A,q+1,r);    }}int main(int argc, char *argv[]){  int  a[] = {7,5,9,46,23,44,31,28,9,7,55,23,17,39,22};  quicksort(a,0,sizeof(a)/sizeof(int)-1);  float  b[] = {7.7,5.9,9.8,46.3,23.2,44.5,31.1,28.9,9.76,7.24,55.22,23,17,39,22};  quicksort(b,0,sizeof(b)/sizeof(float)-1);  return 0;}