zoukankan      html  css  js  c++  java
  • Qsort(c)_Sort(c++)用法

    Sort函数(c)

    (来自codeblocks)

    stdlib.h

    _CRTIMP void __cdecl qsort(void*, size_t, size_t,

                                 int (*)(const void*, const void*));

    (来自网络)

    void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )

    int compare (const void *elem1, const void *elem2 ) );

    qsort(quicksort)主要根据你给的比较条件给一个快速排序,主要是通过指针移动实现排序功能。排序之后的结果仍然放在原来数组中。

    参数意义如下:

    base:需要排序的目标数组开始地址

    num:目标数组元素个数

    目标数组中每一个元素长度

    compare:函数指针,指向比较函数

    Compare 函数的返回值

    描述

    < 0

    elem1将被排在elem2前面

    0

    elem1 等于 elem2

    > 0

    elem1 将被排在elem2后面

    知识点:

    1.

    int cmp(const void *a,const void *b)

    返回值必须为整数(int)

    若a<b,返回:

    负数:升序

    正数:降序

    2.

    *(long *)a

    (long *):指a为(long *)类型

    *a:指获得地址为a的存储单元的数据

    用于:

    目录

    1.一维数组... 2

    整型:... 2

    实型:... 3

    2.一维数组(降序) 4

    3.二维 struct      也可用于一维,如字符串比较... 4

    4.char一维数组... 6

    5.char二维数组... 6

    字符串中str[0]~str[n-1]的值进行排序... 6

    Another Way... 7

    6.注意:... 8

    1.一维数组

    整型:

    #include <stdio.h>

    #include <stdlib.h>

    #define maxn 100

    long a[maxn+1];

    int cmp(const void *a,const void *b)

    {

        if (*(long *)a<*(long *)b)

            return -1;

        else

            return 1;

    //    return (*(long *)a-*(long *)b);

        ///当数为整型,两种方式都可以

        ///而当数不为整型时,只能采用最上面的方法

    }

    int main()

    {

        long n,i;

        scanf("%ld",&n);

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

            scanf("%ld",&a[i]);

        qsort(a,n,sizeof(a[0]),cmp);

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

            printf("%ld ",a[i]);

        return 0;

    }

    /*

    10

    5 3 2 10 1 6 9 4 8 7

    */

    实型:

    当使用*(double *)a-*(double *)b

    如果两者相差0.1,则return 0.1,返回值为0 [返回的数为整数,0.1处理为0,(long)0.1=0],,但事实上并非如此。

    Input:

    5

    0.3 0.2 0.5 0.1 0.4

    Output:

    0.20 0.50 0.10 0.40 0.30

    输出错误。

    #include <stdio.h>

    #include <stdlib.h>

    #define maxn 100

    double a[maxn+1];

    int cmp(const void *a,const void *b)

    {

        if (*(double *)a<*(double *)b)

            return -1;

        else

            return 1;

    //    return (*(double *)a-*(double *)b);

        ///当数为整型,两种方式都可以

        ///而当数不为整型时,只能采用最上面的方法

    }

    int main()

    {

        long n,i;

        scanf("%ld",&n);

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

            scanf("%lf",&a[i]);

        qsort(a,n,sizeof(a[0]),cmp);

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

            printf("%.2lf ",a[i]);

        return 0;

    }

    /*

    5

    0.3 0.2 0.5 0.1 0.4

    */

    2.一维数组(降序)

    与升序相比,cmp中输出改为相反数 或 交换a,b

    #include <stdio.h>

    #include <stdlib.h>

    #define maxn 100

    int cmp(const void *a,const void *b)

    {

        if (*(long *)a<*(long *)b)

            return 1;

        else

            return -1;

    }

    int main()

    {

        long n,a[maxn+1],i;

        scanf("%ld",&n);

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

            scanf("%ld",&a[i]);

        qsort(a,n,sizeof(long),cmp);

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

            printf("%ld ",a[i]);

        return 0;

    }

    /*

    10

    5 3 2 10 1 6 9 4 8 7

    */

    3.二维 struct        也可用于一维,如字符串比较

    #include <stdio.h>

    #include <stdlib.h>

    #define maxn 100

    struct node

    {

        long x,y;

    };

    int cmp(const void *a,const void *b)

    {

        if ((*(struct node *)a).x<(*(struct node *)b).x)

            return -1;

        else if ((*(struct node *)a).x>(*(struct node *)b).x)

            return 1;

        if ((*(struct node *)a).y<(*(struct node *)b).y)

            return -1;

        else

            return 1;

    }

    int main()

    {

        struct node info[maxn+1];

        long n,i;

        scanf("%ld",&n);

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

            scanf("%ld%ld",&info[i].x,&info[i].y);

        qsort(info,n,sizeof(struct node),cmp);

        printf(" ");

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

            printf("%ld %ld ",info[i].x,info[i].y);

        return 0;

    }

    /*

    5

    1 4

    1 2

    3 1

    3 2

    2 1

    */

    4.char一维数组

    字符串中s[0]~s[len-1]的值进行排序

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #define maxlen 100

    int cmp(const void *a,const void *b)

    {

        return *(char *)a-*(char *)b;

    }

    int main()

    {

        long len;

        char s[maxlen+1];

        gets(s);

        len=strlen(s);

        qsort(s,len,sizeof(char),cmp);

        printf("%s ",s);

        return 0;

    }

    /*

    bdcea

    */

    5.char二维数组

    字符串中str[0]~str[n-1]的值进行排序

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #define maxn 100

    #define maxlen 100

    int cmp(const void *a,const void *b)

    {

        return strcmp((char *)a,(char *)b);

    }

    int main()

    {

        //可以 strcmp(char *a,char *b) (char *a) (char *b)

        //*a,*b比较;*(a+1),*(b+1)比较;…………

        //可以这样使用:a[][],等同于*(a+k) (char *a)

        //因为字符从地址a开始依次存储(a,a+1,a+2,……)

        //当使用scanf("%s",s); (char s[])

        //&s即为char *类型,将字符串存储在&s的地址里(以&s为始,&s=s[0])

        //当使用scanf("%s",a); (char *a)

        //&a即为&(char *),为char* 的地址,

        //char *是存储char类型数据的地址的数据结构,存的是整数(地址)

        //所以不能向char *类型(字符的地址)写入数据(字符串)

        long n,i;

        char str[maxn+1][maxlen+1];

        scanf("%ld",&n);

        //去除 数n后面的' '

        gets(str[0]);

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

            gets(str[i]);

        //sizeof(str[0])=maxlen+1

        qsort(str,n,sizeof(str[0]),cmp);

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

            printf("%s ",str[i]);

        return 0;

    }

    /*

    3

    ac

    abcd

    abc

    */

    Another Way

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #define maxn 100

    #define maxlen 100

    //char *的指针(存储char *类型)为char **类型

    //*(char **)a为char *类型

    //可以这样使用strcmp(char *,char *)

    int cmp(const void *a,const void *b)

    {

        return strcmp(*(char **)a,*(char **)b);

    }

    int main()

    {

        long n,i;

        char str[maxn+1][maxlen+1];

        char *pstr[maxlen+1];

        scanf("%ld",&n);

        gets(str[0]);

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

        {

            gets(str[i]);

            pstr[i]=str[i];

        }

        qsort(pstr,n,sizeof(char *),cmp);

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

            printf("%s ",pstr[i]);

        return 0;

    }

    /*

    3

    ac

    abcd

    abc

    */

    6.注意:

    可以排序数组一部分的内容

    #include <stdio.h>

    #include <stdlib.h>

    #define maxn 100

    int cmp(const void *a,const void *b)

    {

        return *(long *)a-*(long *)b;

    }

    int main()

    {

        long a[maxn+1],n,x,y,i;

        scanf("%ld%ld%ld",&n,&x,&y);

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

            scanf("%ld",&a[i]);

        //a为数组a的首地址,a+x-1为数组a中下标为x-1的数的存储地址,即a[x-1]的地址

        //排序a[x]~a[y]的内容

        qsort(a+x-1,y-x+1,sizeof(long),cmp);

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

            printf("%ld ",a[i]);

        return 0;

    }

    /*

    Input:

    5 2 4

    5 4 3 2 1

    Output:

    5 2 3 4 1

    */

    /////////////////////////////////////////////////////////////////////////////////////////

    Sort函数(c++)

    针对不同的排序使用不同的sort

    template <class RandomAccessIterator>

     void sort ( RandomAccessIterator first, RandomAccessIterator last );

    template <class RandomAccessIterator, class Compare>

     void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

    sort(x,y)指的是排序从x开始,从y结束(不包括y)的连续的同类型的内容

    sort中比较函数返回Boolean类型

     

    1.一维数组

    #include <iostream>

    #include <algorithm>

    #define maxn 100

    using namespace std;

    int main()

    {

        long n,a[maxn+1],i;

        cin>>n;

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

            cin>>a[i];

        sort(a,a+n);

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

            cout<<a[i]<<" ";

        return 0;

    }

    2.一维数组(降序)

    #include <iostream>

    #include <algorithm>

    #define maxn 100

    using namespace std;

    bool cmp(long a,long b)

    {

        return a>b;

    }

    int main()

    {

        int n,a[maxn+1],i;

        cin>>n;

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

            cin>>a[i];

        sort(a,a+n,cmp);

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

            cout<<a[i]<<" ";

        return 0;

    }

    3.二维 struct          也可用于一维,如字符串比较

    #include <iostream>

    #include <algorithm>

    #define maxn 100

    using namespace std;

    struct node

    {

        int x,y;

    };

    bool cmp(struct node a,struct node b)

    {

        if (a.x<b.x)

            return 1;

        else if (a.x>b.x)

            return 0;

        if (a.y<b.y)

            return 1;

        else

            return 0;

    }

    int main()

    {

        struct node s[maxn+1];

        int n,i;

        cin>>n;

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

            cin>>s[i].x>>s[i].y;

        sort(s,s+n,cmp);

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

            cout<<s[i].x<<" "<<s[i].y<<endl;

        return 0;

    }

    /*

    5

    1 4

    1 2

    3 1

    3 2

    2 1

    */

    4.char一维数组

    #include <iostream>

    #include <cstring>

    #include <algorithm>

    #define maxlen 100

    using namespace std;

    int main()

    {

        char s[maxlen];

        cin.getline(s,maxlen);

        sort(s,s+strlen(s));

        cout<<s<<endl;

        return 0;

    }

    5.char二维数组

    sort(x,y)指的是排序从x开始,从y结束(不包括y)的连续的同类型的内容

    所以不能sort(str,str+n,cmp),因为str[]的长度无法判断。

    #include <iostream>

    #include <cstring>

    #include <algorithm>

    #define maxn 100

    #define maxlen 100

    using namespace std;

    bool cmp(char *x,char *y)

    {

        if (strcmp(x,y)<0)

            return true;

        else

            return false;

    }

    int main()

    {

        char str[maxn+1][maxlen+1];

        char *pstr[maxlen+1];

        long n,i;

        cin>>n;

        //去除数n后面的' '

        cin.getline(str[0],maxlen);

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

        {

            cin.getline(str[i],maxlen);

            pstr[i]=str[i];

    }

    //改变pstr[x],pstr[y]的值,却不会改变pstr[x]的地址和pstr[y]的地址下的内存的内容

    /*例子:

    地址         内容

    1               ‘a’

    2               ‘c’

    3               ‘’

    10             ‘a’

             11             ‘’

             起始:             pstr[1]=1;                 pstr[2]=10;

             升序排序后:pstr[1]=10;     pstr[2]=1;

    */

        sort(pstr,pstr+n,cmp);

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

            cout<<pstr[i]<<endl;

        return 0;

    }

    /*

    3

    ac

    abcd

    abc

    */

    注意:

    可以排序数组一部分的内容

    #include <iostream>

    #include <algorithm>

    #define maxn 100

    using namespace std;

    int main()

    {

        long n,a[maxn+1],x,y,i;

        cin>>n>>x>>y;

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

            cin>>a[i];

        sort(a+x-1,a+y);

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

            cout<<a[i]<<" ";

        return 0;

    }

    /*

    Input:

    5 2 4

    5 4 3 2 1

    Output:

    5 2 3 4 1

    */

  • 相关阅读:
    sql2005 如何重启数据库服务
    jQuery 树控件 zTree
    谈C#中的Delegate
    微博首席架构师杨卫华:新浪微博技术架构分析(转)
    jqGrid 各种参数 详解
    asp.net(c#)ref,out ,params的区别
    gcc
    数据结构递归
    跳表
    javajvm
  • 原文地址:https://www.cnblogs.com/cmyg/p/6810800.html
Copyright © 2011-2022 走看看