QuickSort模板

#include <iostream>

using namespace std;

struct node

{

         int index;

         char name[20];

};

node data[100];

template<typename T>

void QuickSort(T* Array,int left,int right,bool (*com)(T* a,T* b))

{

         int low,high;

         T key;

         if(left>=right) return;

         low=left,high=right;

         key=Array[left];

         while(low<high)

         {

                   while(low<high && com(&Array[high],&key)) high--;

                   Array[low]=Array[high];

                   while(low<high && com(&key,&Array[low])) low++;

                   Array[high]=Array[low];

         }

         Array[low]=key;

         QuickSort(Array,left,low-1,com);

         QuickSort(Array,low+1,right,com);

}

bool com(int* a,int* b)

{

         return *a>=*b;

}

bool com1(node* a,node* b)

{

         return a->index> b->index;

}

 

int main()

{

         int n,i;

         while(scanf("%d",&n)!=EOF)

         {

                   for(i=0;i<n;i++) scanf("%d %s",&data[i].index,data[i].name);

                   QuickSort(data,0,n-1,com1);

                   for(i=0;i<n;i++) printf("%d %s\n",data[i].index,data[i].name);

                   printf("\n");

         }

         return 0;

}

posted @ 2013-06-28 09:58  书山有路,学海无涯  阅读(421)  评论(0编辑  收藏  举报