qsort 排序函數 总结

qsort包含在<stdlib.h>头文件中,此函数根据你给的比较条件进行快速排序,通过指针移动实现排序。排序之后的结果仍然放在原数组中。使用qsort函数必须自己写一个比较函数。

函数原型:

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

用法以及参数说明:

Sorts the num elements of the array pointed by base, each element size bytes

long, using the comparator function to determine the order.

The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with two pointers to elements
of the array.

The function does not return any value, but modifies the content of the array pointed by base reordering its elements to the newly sorted order.

base Pointer to the first element of the array to be sorted.(数组起始地址)

num Number of elements in the array pointed by base.(数组元素个数)

size Size in bytes of each element in the array.(每一个元素的大小)

comparator Function that compares two elements.(函数指针,指向比较函数)

1、The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.

2、The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.

Return Value none (无返回值)

>>>1. 对整形数据进行排序:(从小到大)

int Num[1010];

qsort(Num, n, sizeof(int), cmp);

int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

从大到小:

int cmp(const void *a, const void *b)
{
    return *(int *)b - *(int *)a;
}

>>>2. 对char數據進行排序:

char str[1010];

qsort(str, sizeof(str), sizeof(char), cmp);

int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

>>>3. 對double數據進行排序:

double Num[1010];

qsort(Num, n, sizeof(double), cmp);

int cmp(const void *a, const void *b)
{
    return *(double *)a > *(double *)b ? 1 : -1;
}

>>>4. 對結構體數據進行排序:

typedef struct Node_
{
    int data;
    int position;
}Node;

Node Num[1010];

qsort(Node, n, sizeof(Node), cmp);

int cmp(const void *a, const void *b)
{
    Node *p1 = (Node *)a;
    Node *p2 = (Node *)b;
    if(p1->data != p2->data) return p1->data - p2->data;
    else return p1->position - p2->position;
}

實例:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define MAXN 1010
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

typedef struct Node_
{
    char name[MAXN];
    int data;
    int position;
}Node;

Node N[MAXN];

int cmp(const void *a, const void *b) //先對data進行從大到小,position和name從小到大排序
{
    Node *p1 = (Node *)a;
    Node *p2 = (Node *)b;
    if(p1->data != p2->data) return p2->data - p1->data;
    else if(p1->position != p2->position) {
        return p1->position - p2->position;
    }else return strcmp(p1->name, p2->name);
}

int main()
{
    int n;
    while(~scanf("%d", &n)) {
        for(int i=0; i<n; i++) {
            scanf("%s %d %d", N[i].name, &N[i].data);
            N[i].position = i;
        }
        qsort(N, n, sizeof(Node), cmp);
        printf("------------------------------------\n");
        for(int i=0; i<n; i++) {
            printf("%s %d %d\n", N[i].name, N[i].data, N[i].position);
        }
    }
    return 0;
}
时间: 2024-08-11 05:44:43

qsort 排序函數 总结的相关文章

用qsort排序

 冒泡,快排都是常见的排序方法,这里介绍用头文件中的qsort函数排序.不过自己要先一个cmp函数. #include<stdlib.h>//qsort的头文件 int a[100]={0,2,4,1,5,7,3,8,9}; //要排序的数组 struct Person//要排序的结构体 { char num[20]; char name[100]; int score; int sum; }man[100]; 1.给数组a[]排序 int cmp_1(const void *a,cons

學習日記:函數和對象

2016-2-21 1. Living without an aim is like sailing without a compass. 生活沒有目標,猶如航海沒有羅盤. 2. 無論是現實世界還是計算機世界,可讀性都是相當重要的,因為這涉及到人們的意識或者是認識效率,一般文字比數字的可理解性和可讀性要好,圖片的可讀性最強. a) 一般數學是比較抽象的,因為其中充滿著各種阿拉伯數字和已經不能再簡化的希臘字符. b) 數學家的得意之作就是覺得自己是在世界科學界的最巔峰. c) 我們能用數學工具處理

對比:莫比烏斯反演與歐拉函數

最近題讓我非常困惑,貌似我現在已經完全分不清楚哪些題用莫比烏斯反演,哪些用歐拉函數. 下面簡單總結一下,莫比烏斯反演處理的是 求segma(gcd(x,y)) 1<=x<=n,1<=y<=m (見<能量項鍊>) gcd(x,y) = k   1<=x<=n 1<=y<=m  求x,y對數 (見<bzoj 2301  problem b>) 莫比烏斯反演原來是解決以上問題2的,大體思路是 設F(a,b,k)表示1<=x<=a

SQL窗口函數一例

需求: MSSQL,列出服務實例中所有數據庫的如下信息: 數據庫ID.數據庫名.創建日期.數據文件類型.數據文件大小.數據庫總大小.文件所在路徑. 寫法(後面的百分比為所花時間占比): -- 连接子查询 (47%) WITH cte_TotalSize AS ( SELECT database_id ,CAST(SUM(size) AS FLOAT)/128 AS [TotalSize(MB)] FROM sys.master_files GROUP BY database_id ) SELEC

qsort排序算法

七种qsort排序方法<本文中排序都是采用的从小到大排序>一.对int类型数组排序int num[100];Sample:int cmp ( const void *a , const void *b ){return *(int *)a - *(int *)b;}qsort(num,100,sizeof(num[0]),cmp);二.对char类型数组排序(同int类型)char word[100];Sample:int cmp( const void *a , const void *b 

SQL窗体函數一例

需求: MSSQL,列出服務實例中全部數據庫的例如以下信息: 數據庫ID.數據庫名.創建日期.數據文件類型.數據文件大小.數據庫總大小.文件所在路徑. 寫法(後面的百分比為所花時間占比): -- 连接子查询 (47%) WITH cte_TotalSize AS ( SELECT database_id ,CAST(SUM(size) AS FLOAT)/128 AS [TotalSize(MB)] FROM sys.master_files GROUP BY database_id ) SEL

ORACLE字符拆分函數,返回結果集

ORACLE不能像MSSQL那樣支持直接返回表類型,所以要先創建一種自定義類型.這裏用到的是嵌套表(Nested Table). -- Nested Table CREATE OR REPLACE TYPE split_str IS TABLE OF VARCHAR(100); / -- Function CREATE OR REPLACE FUNCTION fn_Split ( p_Str VARCHAR2, p_Delimiter VARCHAR2 ) RETURN split_str PI

js匿名函數

(function($){})(jquery) == (function($){})(jQuery) 实际上是匿名函数 用于存放开发插件的代码 作用(非常有用): 这种写法的最大好处是形成闭包.在(function($) {…})(jQuery)在内部定义的函数和变量只能在此范围内有效.   形成是否函数函数.私有变量的概念.比如: var i=3; function init(){ alert("外层init:"+i); } (function($) { var i=2; funct

最易理解的qsort排序算法

快速排序算法程序可以写得千奇百怪,但最易理解的个人认为仍是下面的: #include<stdio.h> #include<time.h> #include<stdlib.h> void swap(int *a ,int *b) { int t = *a; *a = *b; *b = t; } int partition(int array[],int l,int r) { int pivot = array[r]; int curpos = l; int j ; for