qsort排序的基本用法

简介: qsort排序的基本用法
#include<stdio.h>
#include<stdlib.h>//qsort的头文件
int cmp(const void *a,const void *b)
{
 return *(int *)a<*(int *)b;
}
int main()
{
 int m[10],n;
 while(~scanf("%d",&n))
 {
   for(int i=0;i<n;++i)
     scanf("%d",m+i);
   qsort(m,n,sizeof(int),cmp);
  for(int i=0;i<n;++i)
     printf("%d ",*(m+i));
 }
 return 0;
}//  对整型类排序



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmp(const void *a,const void *b)
{
 return strcmp((char *)a,(char *)b);
}
int main()
{
  char m[100][100];
  int n;
   while(~scanf("%d",&n))
   {
     for(int i=0;i<n;++i)
     {
       scanf("%s",m[i]);
     }
     qsort(m,n,sizeof(m[0]),cmp);
     for(int i=0;i<n;++i)
        printf("%s\t",*(m+i));
    }
  return 0; 
 }  // 对字符类排序



#include<stdio.h>
#include<stdlib.h>
typedef struct
{
 int x,y;
}node;
int cmp(const void *a,const void *b)
{
 return (*(node *)a).x>(*(node *)b).x?1:-1;
}
int main()
{
  int n;node m[1010];
  while(~scanf("%d",&n))
  {
    for(int i=0;i<n;++i)
    {
      scanf("%d%d",&m[i].x,&m[i].y);
    }
    qsort(m,n,sizeof(node),cmp);
    for(int i=0;i<n;++i)
    {
       printf("%d %d\t",m[i].x,m[i].y);
    }
 }
 return 0;
} // 对结构体类排序
目录
相关文章
|
23天前
|
存储 人工智能 C++
map容器在C++中的具体用法以及相关注意点
map容器在C++中的具体用法以及相关注意点
19 1
|
1月前
sort排序
sort排序
9 0
|
1月前
排序——sort的用法
排序——sort的用法
14 0
|
1月前
|
机器学习/深度学习 定位技术
最值查找——几种常用函数
最值查找——几种常用函数
14 0
|
1月前
|
C#
C#中sort排序相关用法介绍
C#中sort排序相关用法介绍
|
10月前
数组的相关用法
数组的相关用法
29 0
|
1月前
|
搜索推荐
实现bubble_sort冒泡排序函数,排序任意类型数据
实现bubble_sort冒泡排序函数,排序任意类型数据
37 0
|
1月前
|
C++
C++中sort排序
C++中sort排序
|
1月前
|
小程序
排序sort()排序用法
排序sort()排序用法
|
10月前
|
分布式计算 索引
常见的数组基本用法(二)
常见的数组基本用法
66 0