linux中的 测用时函数

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#define MAX_LENGTH 100
/*Show usage*/
void usage(char * prog)
{
        printf("%s Usage:\n", prog);
        printf("%s <the count of numbers to sort (should be less than 100)>\n", prog);
}
/*Generate and initialize the list*/
int * generate_list(int count)
{
        int i;
        int * list;
        list = (int *)malloc(count*sizeof(int));
        if(list == NULL)
        {
                perror("malloc");
                return NULL;
        }
        /*Initialize the list with integers less than 100*/
        srandom((unsigned int)time(NULL));
        for (i  = 0; i < count ; i ++)
                list[i] = random()%100;
        return list;
}
/*Show the list*/
void show_list(int * list, int length)
{
        int i;
        for(i = 0; i < length; i ++)
                printf("%d      ", list[i]);
        printf("\n");
}
/*algorithm*/
int partition(int * list, int first, int last)
{
        int left = first;
        int right = last;
        int pivot = list[left];//靠靠靠?
        while(left < right)//靠靠靠靠靠靠
        {
                while((right > left) && (list[right] >= pivot))
                        right --;//靠靠?
                if(left < right)
                {
                        list[left] = list[right];//靠靠靠?
                        left ++;
                }
                while((left < right) && (list[left] < pivot))
                        left ++;//靠靠?
                if(left < right)
                {
                        list[right] = list[left];//靠靠靠?
                        right --;
                }
        }
        list[left] = pivot;//靠靠靠?
        return left;
}
void quick_sort(int * list, int first, int last)
{
        if(first < last)//靠靠1
        {
                int split = partition(list, first, last);
			//縧ist靠靠
                quick_sort(list, first, split-1);//靠靠靠靠縮plite靠靠靠
                quick_sort(list, split +1, last);//靠靠靠靠
        }
        return;
}
int main(int argc, char * argv[])
{
        int length;
        int * list = NULL;
        struct timeval tpstart,tpend;
        float timeuse;
        /*Deal with the arguments*/
        if(argc != 2)
        {
                usage(argv[0]);
                exit(127);
        }
        length = atoi(argv[1]);
        if(!length || length > MAX_LENGTH)
        {
                usage(argv[0]);
                exit(129);
        }
        list = generate_list(length);
        if(list == NULL)
                exit(128);
        else
        {
                printf("Soruce list:\n");
                show_list(list, length);
                gettimeofday(&tpstart,NULL);
                quick_sort(list, 0, length -1);
                gettimeofday(&tpend,NULL);
                printf("Ordered list:\n");
                show_list(list, length);
                timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec;
                timeuse/=1000;
                printf("quick_sort time used: %f msec\n", timeuse);
        }
        free(list);
        return 1;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值