zoukankan      html  css  js  c++  java
  • 快速排序--按姓名

    //按姓名快速排序

    #include <stdio.h>

    #include <string.h>

    #define N 10

    typedef struct student

    {

        int num;

        char name[20];

        char sex[2];

        int age;

    }stu[N];

    int quickpartition(struct student stu[],int low,int high)

    {

        struct student x;

        x=stu[low];

        while(low<high)

        {

            while((low<high)&&(strcmp(stu[high].name,x.name)<=0))

                high--;

            stu[low]=stu[high];

            while((low<high)&&(strcmp(stu[low].name,x.name)>=0))

                low++;

            stu[high]=stu[low];

        }

        stu[low]=x;

        return high;

    }

    void quicksort(struct student stud[],int low,int high)

    {

        int temp;

        if(low<high)

        {

            temp=quickpartition(stud,low,high);

            quicksort(stud,low,temp-1);

            quicksort(stud,temp+1,high);

        }

    }

    int main()

    {

        struct student stu1[4]={{1001,"zhang","m",19},

            {1002,"chen","f",20},

            {1003,"ma","m",20},

            {1004,"sun","m",18}};

        int len,i;

        len=sizeof(stu1)/sizeof(stu1[0]);

        quicksort(stu1,0,len-1);

        for(i=0;i<len;i++)

        {

            printf(" %d %s %s %d ",stu1[i].num,stu1[i].name,stu1[i].sex,stu1[i].age);

        }

        return 1;

    }

    运行结果:

  • 相关阅读:
    Flash与JS之间相互调用以及参数传递
    在Javascript中监听flash事件(转)
    DataTables.js插入数据的两种方式
    DataTable在内存中的使用
    oracle分页存储过程
    清空highcharts数据
    JDBC源码分析(加载过程)
    MAVEN实现多环境搭建
    Maven基础
    Ajax如何提交数据到springMVC后台
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11119933.html
Copyright © 2011-2022 走看看