zoukankan      html  css  js  c++  java
  • 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 ",data[i].index,data[i].name);

                       printf(" ");

             }

             return 0;

    }

  • 相关阅读:
    smb上传图片工具类
    hzero
    ORACLE
    数据库范式
    数据库设计阶段
    Java变量和运算符
    相对路径和绝对路径
    setTimeout()方法和setInterval()方法
    body onload()事件和table insertRow()、tr insertCell()
    eval函数和isNaN函数
  • 原文地址:https://www.cnblogs.com/yu-chao/p/3160288.html
Copyright © 2011-2022 走看看