今天在看高手的代碼的時候才發現,C語言中有一個qsort的庫函數(在<stdlib.h>標頭檔中),該函數可以對使用者指定的資料(或者說記憶體地區)進行快速排序,為什麼說是對記憶體地區排序呢?因為排序的過程中是對使用者指定的記憶體地區按照使用者指定的位移量進行排序的,所以不僅可以對簡單類型的數組排序,也可以對結構體類型的數組按照其某個欄位來排序,使用起來非常的方便,下面將詳細說明該函數的用法。
另外,C++的STL中也實現並最佳化了排序函數,並提供了sort、stable_sort等不同的版本,且這些函數都是泛型函數,可以對不同的容器進行排序,功能比C語言版本的qsort要強大,效率要高。(可以參考文章《詳細解說 STL 排序(Sort)》:http://www.cppblog.com/mzty/archive/2005/12/15/1770.html)
=================================================================================================================================
以下內容轉自:http://www.slyar.com/blog/stdlib-qsort.html
C語言標準庫函數 qsort
詳解
文章作者:薑南(Slyar)
文章來源:Slyar Home (www.slyar.com)
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 pointedby base, each element size bytes long, using the comparator function todetermine the order.
The sorting algorithm used by this functioncompares pairs of values by calling the specified comparator function with twopointers to elements of the array.
The function does not return any value, butmodifies the content of the array pointed by base reordering its elements tothe newly sorted order.
base Pointer tothe first element of the array to be sorted.(數組起始地址)
num Number of elements in the arraypointed by base.(數組元素個數)
size Size in bytes of each element in thearray.(每一個元素的大小)
comparator Function that compares two elements.(函數指標,指向比較函數)
1、The function must accept two parameters thatare pointers to elements, type-casted as void*. These parameters should be castback to some data type and be compared.
2、The return value of this function shouldrepresent whether elem1 is considered less than, equal to, or greater thanelem2 by returning, respectively, a negative value, zero or a positive
value.
Return Value none (無傳回值)
一、對int類型數組排序
int num[100];
int cmp (const void *a , const void *b )
{
return *(int *)a - *(int *)b;
}
qsort(num,100,sizeof(num[0]),cmp);
二、對char類型數組排序(同int類型)
charword[100];
int cmp(const void *a , const void *b )
{
return *(char *)a - *(int *)b;
}
qsort(word,100,sizeof(word[0]),cmp);
三、對double類型數組排序
double in[100];
int cmp(const void *a , const void *b )
{
return *(double *)a > *(double *)b ? 1 : -1;
}
qsort(in,100,sizeof(in[0]),cmp);
四、對結構體一級排序
struct Sample
{
double data;
int other;
}s[100]
//按照data的值從小到大將結構體排序
int cmp(const void *a ,const void *b)
{
return (*(Sample *)a).data > (*(Sample *)b).data ? 1 : -1;
}
qsort(s,100,sizeof(s[0]),cmp);
五、對結構體二級排序
struct Sample
{
int x;
int y;
}s[100];
//按照x從小到大排序,當x相等時按照y從大到小排序
int cmp(const void *a , const void *b )
{
struct Sample *c = (Sample *)a;
struct Sample *d = (Sample *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
}
qsort(s,100,sizeof(s[0]),cmp);
六、對字串進行排序
struct Sample
{
int data;
char str[100];
}s[100];
//按照結構體中字串str的字典順序排序
int cmp (const void *a , const void *b )
{
return strcmp( (*(Sample *)a).str , (*(Sample *)b).str );
}
qsort(s,100,sizeof(s[0]),cmp);
附加一個完整點的代碼,對字串二維數組排序:
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
chars[2001][1001];
int cmp(constvoid *a, const void *b){
return strcmp((char *)a,(char *)b);
}
int main(){
int i,n;
scanf("%d",&n);
getchar();
for(i=0;i<n;i++) gets(s[i]);
qsort(s,n,1001*sizeof(char),cmp);
for(i=0;i<n;i++) puts(s[i]);
return 0;
}
=================================================================================================================================
http://blog.163.com/sentimental_man/blog/static/73001618200941584052485/該文章中使用qsort函數對線段數組進行排序,數組中存的是線段的起點終點的值,每條線段佔兩個int,自己編寫的cmp函數以起點值進行比較,所以最終是以線段的起點進行排序的。該文章中,作者標註了“qsort(map,
n, sizeof(int)*2, cmp); /*這兒行嗎?map? 不是一維數組啊?*/”,實際上這裡是沒有問題的,而恰恰是對qsort的巧妙使用!