機能 |
クリックソート(並び替え)を行う。 |
||||||||
---|---|---|---|---|---|---|---|---|---|
定義 |
#include <stdlib.h> void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void *) ); |
||||||||
引数 |
|
||||||||
実装例 |
#include <stdlib.h> #include <stdio.h> /* 比較関数 */ int compare(const void *a, const void *b) { int an = *(int*)(a); int bn = *(int*)(b); if (an < bn) { return -1; } else if (an > bn) { return +1; } else { return 0; } } int main(void) { int list[10] = {653, 67, 214, 87, 804, 125, 12, 153, 234, 90}; int i; /* ソート前を表示 */ for (i=0; i<10; i++) { printf("%d, ", list[i]); } printf("\n"); /* クリックソート */ qsort(list, 10, sizeof(int), compare); /* ソート後を表示 */ for (i=0; i<10; i++) { printf("%d, ", list[i]); } printf("\n"); return 0; } |
||||||||
実行結果 |
653, 67, 214, 87, 804, 125, 12, 153, 234, 90, 12, 67, 87, 90, 125, 153, 214, 234, 653, 804, |
qsort()