本程序目的是测试qsort函数的用法 

qsort原型:

void qsort(void *base, size_t nmemb, size_t size,
                  int (*compar)(const void *, const void *));

 qsort()函数排序一个尺寸为size大小的numb个元素的数组。

其中参数含义如下:base参数指向这个数组的首地址。根据由compar指向的比较函数用升序排序这个数组的内容。compar函数有两个参数,它们指向被比较的对象。如果第一个参数被认为小于,等于或者大于第二个参数,这个比较函数必须返回一个小于,等于或者大于0的整数。如果两个成员被认为相等,它们在排序好数组中的顺序不不确定的。

 以下是一个函数示例:此程序随机生成一个int元素的数组,其大小和元素都是随机生成的,数组元素个数为5-10之间,元素大小为1-100之间。程序打印在排序前后这个数组。

rand_string函数产生一个长度在min和max之间由小写字母组成的字符串,这个字符串由str返回。

compareVector函数比较类型为Vector的矢量的长度。

#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>

/*
        void qsort(void *base, size_t nmemb, size_t size,
                  int (*compar)(const void *, const void *));
        void *malloc(size_t size);
        void free(void *ptr);

*/
char * STRS = "abcdefghijklmnopqrstuvwxyz";

int rand_string(int min, int max, char * str, int length)
{
        int counts, i;
        char * ptr;

        if (min <1 || min >=  max || max > length -1)
        {
                return -1;
        }

        counts = rand() % (min + 1) + (max - min);

        for (i = 0; i < counts; i++)
        {
                ptr = STRS + (rand() % 26);
                * (str + i) = * ptr;
        }

        * (str + counts) = '\0';

        return 0;
}



int compareFunc(const void * a, const void * b)
{
        int x, y;

        x = * (int *)a;
        y = * (int *)b;

        return (x - y);
}

typedef struct vector{
        float x;
        float y;
}Vector;

//根据矢量的长度,对矢量进行排序
int compareVector(const void * a, const void * b)
{
        Vector * pa, * pb;
        float s1, s2;
        pa = (Vector *)a;
        pb = (Vector *)b;

        s1 = pa->x * pa->x  + pa->y * pa->y;
        s2 = pb->x * pb->x  + pb->y * pb->y;

        return (int)(s1 - s2);
}

int main(int argc, char * argv[])
{
        int counts;
        int * numbers, * numptr;
        int i;
        Vector * vptr, * vectors;

        srand(time(0));

        counts = rand() % 6 + 5; //counts ranges from 5 to 10

        numbers = (int *)malloc(sizeof(int) * counts);
        numptr = numbers;

        for (i = 0; i < counts; i++)
        {
                * numptr = rand() % 100 + 1; //element from 1 to 100
                numptr++;
        }

        printf("Before qsort: ");
        numptr = numbers;
        for (i = 0; i < counts; i++)
        {
                printf("%-5d",*numptr );
                numptr++;
        }
        printf("\n\n");

        qsort(numbers, counts, sizeof(int), compareFunc);

        printf("After qsort: ");
        numptr = numbers;
        for (i = 0; i < counts; i++)
        {
                printf("%-5d",*numptr );
                numptr++;
        }
        printf("\n\n");

        free(numbers);

        char strs[5][20];

        for (i = 0; i < 5; i++)
        {
                rand_string(5,15, strs[i], 20);
        }

        for (i = 0; i < 5; i++)
        {
                printf("%s\n", strs[i]);
        }


        counts = rand() % 6 + 5; //counts ranges from 5 to 10
        vectors = (Vector *)malloc(sizeof(Vector) * counts);
        vptr = vectors;

        for (i = 0; i < counts; i++)
        {
                vptr->x = rand() % 20 + 1;
                vptr->y = rand() % 20 + 1;
                vptr++;
        }

        printf("Vectors before qsort:");
        vptr = vectors;
        for (i = 0; i < counts; i++)
        {
                printf("Vector(%.2f,%.2f)\n", vptr->x,vptr->y);
                vptr++;
        }
        printf("\n");

        printf("Vectors after qsort:");
        qsort(vectors, counts, sizeof(Vector), compareVector);

        vptr = vectors;
        for (i = 0; i < counts; i++)
        {
                printf("Vector(%.2f,%.2f)\n", vptr->x,vptr->y);
                vptr++;
        }
        printf("\n");

        free(vectors);

        return 0;
}

用gcc编译后,运行这个可执行文件,编译和测试如下:

1) 产生随机长度的随机整数数组,并对其进行排序。

2) 产生长度随机的小写字母组成的数组

3) 产生随机长度的Vector类型数组,Vector中成员变量的值也是随机产生,并对这个Vector数组排序。

[blctrl@rockygu C_Program]$ gcc -o qsort qsort.c
[blctrl@rockygu C_Program]$ ./qsort
Before qsort: 6    9    97   20   22

After qsort: 6    9    20   22   97

bnnbysgymxy
piywrhbnwlx
owakpntrcgubbb
pazrpkplbypqu
naolaabntuqcot
Vectors before qsort:Vector(10.00,13.00)
Vector(9.00,14.00)
Vector(10.00,16.00)
Vector(2.00,16.00)
Vector(9.00,5.00)
Vector(9.00,7.00)
Vector(18.00,3.00)
Vector(20.00,13.00)
Vector(10.00,8.00)
Vector(3.00,12.00)

Vectors after qsort:Vector(9.00,5.00)
Vector(9.00,7.00)
Vector(3.00,12.00)
Vector(10.00,8.00)
Vector(2.00,16.00)
Vector(10.00,13.00)
Vector(9.00,14.00)
Vector(18.00,3.00)
Vector(10.00,16.00)
Vector(20.00,13.00)

[blctrl@rockygu C_Program]$ ./qsort
Before qsort: 44   79   11   18   84   95   10   84

After qsort: 10   11   18   44   79   84   84   95

asebytmxybmim
ytwhugqyhgql
julqwfwadpmcop
wchuxfdtmwrtck
gtbtlxyixdxk
Vectors before qsort:Vector(18.00,10.00)
Vector(17.00,14.00)
Vector(6.00,10.00)
Vector(17.00,11.00)
Vector(18.00,10.00)

Vectors after qsort:Vector(6.00,10.00)
Vector(17.00,11.00)
Vector(18.00,10.00)
Vector(18.00,10.00)
Vector(17.00,14.00)
12-08 22:06