Compiler Explorer - C (x86-64 gcc 12.1)
int compare(void const * a, void const * b) // 定义比较函数 { return (*(int *)a) - (*(int *)b); // 示例为从小到大排列 } int main(void) { int source[] = {12, 8, 4, 23, 5}; // 这里演示部分排序 qsort(source + 1, // 首地址:从 source[1] 开始排序 3, // 元素个数:排序3个元素 sizeof(source[1]), // 每个元素的大小 compare // 比较函数 ); // 这里 source[1] 至 source[3] 已经完成排序 // source 数组现在为 {12, 4, 8, 23, 5} return 0; }
https://godbolt.org/z/1sbhW4fKs