c语言qsort函数源码的简单介绍

C语言当中的qsort函数功 能: 使用快速排序例程进行排序
用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
参数:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针 , 用于确定排序的顺序
其实c中的函数不用死记 , 知道有这个函数及其功能就可以了,然后有用的时候,具体参数查一下就可以了 。
希望能帮到你
急求?。。∮胏语言编写一个qsort函数,要求是对一个二位数组按每一个元素最后一个元素从小到大排序先看程序的运行结果,看是不是你需要的 。
代码如下,有不清楚的可以追问
#include stdio.h
#include stdlib.h
#include string.h
int printArray(int array[][2], int dim1, int dim2 );
int swap(int *a, int *b);
int qsort(int array[][2]);
int main()
{
int array[6][2] = {4, 9, 2, 6, 4, 5, 1, 2, 1, 7, 2, 4};
printf("排序前:\n");
printArray(array, 6, 2);
qsort(array);
printf("排序后:\n");
printArray(array, 6, 2);
return 0;
}
int qsort(int array[][2])
{
int i = 0;
int j = 0;
for( i = 0; i6; i)
{
for(j = i 1; j6; j)
{
if(*(*(array i) 1)*(*(array j) 1))
{
swap(*(*(array i) 0), *(*(array j) 0));
swap(*(*(array i) 1), *(*(array j) 1));
}
}
}
return 0;
}
int swap(int *a, int *b)
{
*a = (*a)(*b);
*b = (*a) - (*b);
*a = (*a) - (*b);
return 0;
}
int printArray(int array[][2], int dim1, int dim2 )
{
int i = 0;
int j = 0;
for( i = 0; idim1; i)
{
for(j = 0; jdim2; j)
{
printf("%d ", *(*(array i) j));
}
printf("\n");
}
return 0;
}
C语言中qsort函数怎么用?qsort函数是编译器函数库自带的快速排序函数 。
qsort 的函数原型是:
void qsort(void*base,size_t num,size_t width,int(__cdecl*compare)(const void*,const void*));
它的参数为: 1 待排序数组首地址
2 数组中待排序元素数量
3 各元素的占用空间大小
4 指向函数的指针 , 用于确定排序的顺序
它在使用的时候需要包含头文件:stdlib.h
实例:
#includestdio.h
#includestdlib.h
int comp(const void*a,const void*b)
{
return *(int*)a-*(int*)b;
}
int main()
{
int *array;
int n;
scanf("%d",n);
array=(int*)malloc(n*sizeof(int));
int i=0;
for(;in;i)
{
scanf("%d",(array i));
}
qsort(array,n,sizeof(int),comp);
for(i=0;in;i)
{
printf("%d\t",array[i]);
}
return0;
}
C语言库函数qsort源代码void __fileDECL qsort (
void *base,
size_t num,
size_t width,
int (__fileDECL *comp)(const void *, const void *)
)
#endif/* __USE_CONTEXT */
{
char *lo, *hi;/* ends of sub-array currently sorting */
char *mid;/* points to middle of subarray */
char *loguy, *higuy;/* traveling pointers for partition step */
size_t size;/* size of the sub-array */
char *lostk[STKSIZ], *histk[STKSIZ];
int stkptr;/* stack for saving sub-array to be processed */
/* validation section */
_VALIDATE_RETURN_VOID(base != NULL || num == 0, EINVAL);
_VALIDATE_RETURN_VOID(width0, EINVAL);
【c语言qsort函数源码的简单介绍】_VALIDATE_RETURN_VOID(comp != NULL, EINVAL);
if (num2)
return;/* nothing to do */
stkptr = 0;/* initialize stack */
lo = (char *)base;
hi = (char *)basewidth * (num-1);/* initialize limits */
/* this entry point is for pseudo-recursion calling: setting
lo and hi and jumping to here is like recursion, but stkptr is
preserved, locals aren't, so we preserve stuff on the stack */
recurse:
size = (hi - lo) / width1;/* number of el's to sort */
/* below a certain size, it is faster to use a O(n^2) sorting method */
if (size = CUTOFF) {
__SHORTSORT(lo, hi, width, comp, context);
}
else {
/* First we pick a partitioning element.The efficiency of the
algorithm demands that we find one that is approximately the median
of the values, but also that we select one fast.We choose the
median of the first, middle, and last elements, to avoid bad
performance in the face of already sorted data, or data that is made
up of multiple sorted runs appended together.Testing shows that a
median-of-three algorithm provides better performance than simply
picking the middle element for the latter case. */
mid = lo(size / 2) * width;/* find middle element */
/* Sort the first, middle, last elements into order */
if (__COMPARE(context, lo, mid)0) {
swap(lo, mid, width);
}
if (__COMPARE(context, lo, hi)0) {
swap(lo, hi, width);
}
if (__COMPARE(context, mid, hi)0) {
swap(mid, hi, width);
}
/* We now wish to partition the array into three pieces, one consisting
of elements = partition element, one of elements equal to the
partition element, and one of elementsthan it.This is done
below; comments indicate conditions established at every step. */
loguy = lo;
higuy = hi;
/* Note that higuy decreases and loguy increases on every iteration,
so loop must terminate. */
for (;;) {
/* lo = loguyhi, lohiguy = hi,
A[i] = A[mid] for lo = i = loguy,
A[i]A[mid] for higuy = ihi,
A[hi] = A[mid] */
/* The doubled loop is to avoid calling comp(mid,mid), since some
existing comparison funcs don't work when passed the same
value for both pointers. */
if (midloguy) {
do{
loguy= width;
} while (loguymid__COMPARE(context, loguy, mid) = 0);
}
if (mid = loguy) {
do{
loguy= width;
} while (loguy = hi__COMPARE(context, loguy, mid) = 0);
}
/* lologuy = hi 1, A[i] = A[mid] for lo = iloguy,
either loguyhi or A[loguy]A[mid] */
do{
higuy -= width;
} while (higuymid__COMPARE(context, higuy, mid)0);
/* lo = higuyhi, A[i]A[mid] for higuyihi,
either higuy == lo or A[higuy] = A[mid] */
if (higuyloguy)
break;
/* if loguyhi or higuy == lo, then we would have exited, so
A[loguy]A[mid], A[higuy] = A[mid],
loguy = hi, higuylo */
swap(loguy, higuy, width);
/* If the partition element was moved, follow it.Only need
to check for mid == higuy, since before the swap,
A[loguy]A[mid] implies loguy != mid. */
if (mid == higuy)
mid = loguy;
/* A[loguy] = A[mid], A[higuy]A[mid]; so condition at top
of loop is re-established */
}
/*A[i] = A[mid] for lo = iloguy,
A[i]A[mid] for higuyihi,
A[hi] = A[mid]
higuyloguy
implying:
higuy == loguy-1
or higuy == hi - 1, loguy == hi1, A[hi] == A[mid] */
/* Find adjacent elements equal to the partition element.The
doubled loop is to avoid calling comp(mid,mid), since some
existing comparison funcs don't work when passed the same value
for both pointers. */
higuy= width;
if (midhiguy) {
do{
higuy -= width;
} while (higuymid__COMPARE(context, higuy, mid) == 0);
}
if (mid = higuy) {
do{
higuy -= width;
} while (higuylo__COMPARE(context, higuy, mid) == 0);
}
/* OK, now we have the following:
higuyloguy
lo = higuy = hi
A[i]= A[mid] for lo = i = higuy
A[i]== A[mid] for higuyiloguy
A[i]A[mid] for loguy = ihi
A[hi] = A[mid] */
/* We've finished the partition, now we want to sort the subarrays
[lo, higuy] and [loguy, hi].
We do the smaller one first to minimize stack usage.
We only sort arrays of length 2 or more.*/
if ( higuy - lo = hi - loguy ) {
if (lohiguy) {
lostk[stkptr] = lo;
histk[stkptr] = higuy;
stkptr;
}/* save big recursion for later */
if (loguyhi) {
lo = loguy;
goto recurse;/* do small recursion */
}
}
else {
if (loguyhi) {
lostk[stkptr] = loguy;
histk[stkptr] = hi;
stkptr;/* save big recursion for later */
}
if (lohiguy) {
hi = higuy;
goto recurse;/* do small recursion */
}
}
}
/* We have sorted the array, except for any pending sorts on the stack.
Check if there are any, and do them. */
--stkptr;
if (stkptr = 0) {
lo = lostk[stkptr];
hi = histk[stkptr];
goto recurse;/* pop subarray from stack */
}
else
return;/* all subarrays done */
}
c语言qsort函数问题您好:
回答问题a:调用的是函数qsort,包含在库stdlib.h中 , 该函数的主要内容是用快速排序算法对目标进行排序,排序的规则由最后一个参数决定,sum函数就是一个普通函数 , 这里用b-a,表示的是使用降序的规则对目标进行排序;传递的是两个常量指针,参数是由qsort函数在其实现过程中传入 。
问题b:当函数没有“()”时候,表示函数的地址,也就是说,在使用qsort函数的时候 , 传入了sum的函数地址,让qsort找到sum的实现 , 并调用它 。
回答完毕,有何不解可追问 , 望笑纳 ^_^
C语言中qsort函数用法详解:1.该函数属于#include stdlib.h标准库中,且是快速排序;
2.qsort函数声明c语言qsort函数源码:void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*));
3.*base:指向要排序数组c语言qsort函数源码的第一个元素指针,而数组名则是该数组的起始地址;size_t nitems:指数组中元素个数;size_t size:指数组中单个元素的大?。籧mp:指用来比较两个元素的函数(一般根据需求自己重写);
4.实例:1.定义一结构体:
struct Interval{
int x, y;
}I[110];
2.重写qsort函数
int cmp(const void *a, const void *b){
if(((const struct Interval *)a)-x != ((const struct Interval *)b)-x)
return ((const struct Interval *)b)-x((const struct Interval *)a)-x; //先按左端点从大到小排列
else return ((const struct Interval *)a)-y((const struct Interval *)b)-y; //然后按左端点相同的右端点从小到大排序
}
1.((const struct Interval *)b)是指针类型2.return的值与需求相反(具体可看上文)
3.main函数里调用:
qsort(I, n, sizeof(struct Interval), cmp); (结构体的单个元素大小与其c语言qsort函数源码他标准类型不同) 。
关于c语言qsort函数源码和的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读