函數原型
void sort(void *base,
size_t num,
size_t size,
int (*cmp_func)(const void *, const void *),
void (*swap_func)(void *, void *, int size))
參數說明
base:數據的基地址
num:數據的個數
size: 每個項的大小
cmp_fun:自定義比較函數
swap_func:自定義交換函數
使用舉例
#include <linux/kmod.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
//sort()函數的頭文件
#include <linux/sort.h>
//自定義排序函數 從小到大排序
int my_cmp(const void *a,const void *b)
{
int *da1 = (int *)a;
int *da2 = (int *)b;
if(*da1 > *da2)
return 1;
else if(*da1 < *da2)
return -1;
else
return 0;
}
static int __init sort_init(void)
{
int i = 0;
int a[10]={1,4,2,3,1,5,6,8,7,9};
//調用排序函數 堆排序
sort(a,10,sizeof(int),my_cmp,NULL);
//將排序後的結果進行打印
for( i=0; i<10; i++ )
printk("%d ",a[i]);
return 0;
}
static void __exit sort_exit(void)
{
return;
}
module_init(sort_init);
module_exit(sort_exit);
MODULE_LICENSE("GPL");