qsort的三个例子

//对字符串内部按照字符大小进行排序

#include <stdlib.h>

#include <string.h>

#include <stdio.h>



int compare( const void *arg1, const void *arg2 );



int main( int argc, char **argv )

{



   char a[255]="18AWSD723adasdf649";

   int k=strlen(a);



   qsort((void *)a,(size_t)strlen(a),sizeof(char),compare);

   printf  ("%s",a);

}



int compare( const void *arg1, const void *arg2 )

{

 //return -(((char *)arg1)[0]-((char*)arg2)[0]);//降序,这个值取负则为升序

 return ((char*)arg1)[0] - ((char*)arg2)[0];

}





//对字符串数组进行排序

#include<stdlib.h>

#include <string.h>

#include <stdio.h>

static int comp(const void * ele1, const void * ele2)

{

    return strcmp(*(const char ** ) ele1, *(const char** )ele2);

}



int main()

{

    char* str[5]=

    {   "sdf",

        "fff",

        "ttt",

        "12aaa",

        "erefrerw"

    };

    int i=0;

    qsort(str, 5, sizeof(char*), comp) ;

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

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

    return 0;

}





//对整型数组进行排序

#include <stdio.h>

#include <stdlib.h>



int values[] = { 40, 10, 100, 90, 20, 25 };



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

{

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

}



int main ()

{

  int n;

  qsort (values, 6, sizeof(int), compare);

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

     printf ("%d ",values[n]);

  return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值