zoukankan      html  css  js  c++  java
  • qsort函数的用法

    function
    <cstdlib>

    qsort

    void qsort (void* base, size_t num, size_t size,
                int (*compar)(const void*,const void*));
    各参数:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针

    Sort elements of array

    Sorts the num elements of the array pointed to by base, each element size bytes long, using the compar function to determine the order.

    The sorting algorithm used by this function compares pairs of elements by calling the specified compar function with pointers to them as argument.

    The function does not return any value, but modifies the content of the array pointed to by base reordering its elements as defined by compar.

    The order of equivalent elements is undefined.

    Parameters

    base
    Pointer to the first object of the array to be sorted, converted to a void*.
    num
    Number of elements in the array pointed to by base.
    size_t is an unsigned integral type.
    size
    Size in bytes of each element in the array.
    size_t is an unsigned integral type.
    compar
    Pointer to a function that compares two elements.
    This function is called repeatedly by qsort to compare two elements. It shall follow the following prototype:
     
    int compar (const void* p1, const void* p2);
     

    Taking two pointers as arguments (both converted to const void*). The function defines the order of the elements by returning (in a stable and transitive manner):
    return value meaning
    <0 The element pointed to by p1 goes before the element pointed to by p2
    0 The element pointed to by p1 is equivalent to the element pointed to by p2
    >0 The element pointed to by p1 goes after the element pointed to by p2

    For types that can be compared using regular relational operators, a general compar function may look like:

    1
    2
    3
    4
    5
    6
    
    int compareMyType (const void * a, const void * b)
    {
      if ( *(MyType*)a <  *(MyType*)b ) return -1;
      if ( *(MyType*)a == *(MyType*)b ) return 0;
      if ( *(MyType*)a >  *(MyType*)b ) return 1;
    }
     


    Return Value

    none

    (本文默认采用从小到大排序)

    一、对int类型数组排序

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define N 10
    
    int cmp(const void *a, const void *b)
    {
    	return *((int*)a) - *((int*)b);
    }
    
    int main()
    {
    	srand((unsigned)time(NULL));
    	int num[100];
    	for (int i = 0; i < N; i++)
    	{
    		num[i] = rand() % 100;
    	}
    	puts("待排序数组:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%d ", num[i]);
    	}
    	putchar('
    ');
    	qsort(num, N, sizeof(num[0]), cmp);
    	puts("排序后:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%d ", num[i]);
    	}
    }

    二、对char类型数组排序(同int类型)

    三、对double类型数组排序

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define N 10
    
    int cmp(const void *a, const void *b)
    {
    	return *((double*)a) > *((double*)b) ? 1 : -1;
    }
    
    int main()
    {
    	srand((unsigned)time(NULL));
    	double num[100];
    	for (int i = 0; i < N; i++)
    	{
    		num[i] = rand() % 100 / 2.0;
    	}
    	puts("待排序数组:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%.1f ", num[i]);
    	}
    	putchar('
    ');
    	qsort(num, N, sizeof(num[0]), cmp);
    	puts("排序后:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%.1f ", num[i]);
    	}
    }

    四、对结构体一级排序

    (按照chinese的数值从小到大排序)
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define N 10
    
    typedef struct SCORE
    {
    	int chinese;
    	int math;
    	int english;
    }SCORE;
    
    int cmp(const void *a, const void *b)
    {
    	return ((SCORE*)a)->chinese - ((SCORE*)b)->chinese;
    }
    
    int main()
    {
    	srand((unsigned)time(NULL));
    	
    	SCORE scores[100];
    	for (int i = 0; i < N; i++)
    	{
    		scores[i].chinese = rand() % 100;
    		scores[i].math = rand() % 100;
    		scores[i].english = rand() % 100;
    	}
    	puts("待排序数据:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%d %d %d
    ", scores[i].chinese, scores[i].math, scores[i].english);
    	}
    	putchar('
    ');
    	qsort(scores, N, sizeof(SCORE), cmp);
    	puts("排序后:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%d %d %d
    ", scores[i].chinese, scores[i].math, scores[i].english);
    	}
    }


    五、对结构体二级排序

    (按照chinese从小到大排序,相同时按照math从小到大排序)
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define N 10
    
    typedef struct SCORE
    {
    	int chinese;
    	int math;
    	int english;
    }SCORE;
    
    int cmp(const void *a, const void *b)
    {
    	if (((SCORE*)a)->chinese != ((SCORE*)b)->chinese)
    	{
    		return ((SCORE*)a)->chinese - ((SCORE*)b)->chinese;
    	}
    	else
    	{
    		return ((SCORE*)a)->math - ((SCORE*)b)->math;
    	}
    }
    
    int main()
    {
    	srand((unsigned)time(NULL));
    	
    	SCORE scores[100];
    	for (int i = 0; i < N; i++)
    	{
    		scores[i].chinese = rand() % 100;
    		scores[i].math = rand() % 100;
    		scores[i].english = rand() % 100;
    	}
    	puts("待排序数据:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%d %d %d
    ", scores[i].chinese, scores[i].math, scores[i].english);
    	}
    	putchar('
    ');
    	qsort(scores, N, sizeof(SCORE), cmp);
    	puts("排序后:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%d %d %d
    ", scores[i].chinese, scores[i].math, scores[i].english);
    	}
    }


    六、对字符串进行排序

    (按照name字典序进行排序)
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 3
    
    typedef struct PERSON
    {
    	char name[20];
    	int age;
    }PERSON;
    
    int cmp(const void *a, const void *b)
    {
    	return strcmp(((PERSON*)a)->name, ((PERSON*)b)->name);
    }
    
    int main()
    {
    	
    	PERSON persons[N];
    	strcpy(persons[0].name, "aab");
    	persons[0].age = 10;
    	strcpy(persons[1].name, "abc");
    	persons[1].age = 11;
    	strcpy(persons[2].name, "aaa");
    	persons[2].age = 12;
    	puts("待排序数据:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%s %d
    ", persons[i].name, persons[i].age);
    	}
    	putchar('
    ');
    	qsort(persons, N, sizeof(PERSON), cmp);
    	puts("排序后:");
    	for (int i = 0; i < N; i++)
    	{
    		printf("%s %d
    ", persons[i].name, persons[i].age);
    	}
    }


    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    处在什么都想学,却又不知道怎么学的处境
    启动MongoDB shell客户端会什么会一闪而过
    Socket.io发送消息含义
    轮询、长轮询与Web Socket的前端实现
    org.apache.commons.lang3.StringUtils类中isBlank和isEmpty方法的区别
    JS学习笔记10_Ajax
    JS学习笔记9_JSON
    JS学习笔记8_错误处理
    JS学习笔记7_表单脚本
    JS学习笔记6_事件
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834709.html
Copyright © 2011-2022 走看看