数据结构与算法分析1.1 选择问题

数据结构与算法分析-C语言实现,1.1题 实现选择问题,并打印不同N规模是运行时间:

用了自己实现的快速排序法,各个N规模显示如下:

root@ltegan:chapter1# ./a.out 
Before choose issue clock() returns: 27834 clocks-per-sec (0.03 sec)
times() yields: user CPU:0.02; system CPU:0.00
Your K[500000] max is :50 
After choose issue clock() returns: 12527475 clocks-per-sec (12.53 sec)
times() yields: user CPU:12.52; system CPU:0.00 <<<<大部分时间都是在跑用户空间的代码
N:1000000, clock:12499641(12.50 sec) <<<<<<<这里使用了12.5秒
Before choose issue clock() returns: 12527596 clocks-per-sec (12.53 sec)
times() yields: user CPU:12.52; system CPU:0.00
Your K[500] max is :49 
After choose issue clock() returns: 12527711 clocks-per-sec (12.53 sec)
times() yields: user CPU:12.52; system CPU:0.00
N:1000, clock:115(0.00 sec)<<<<<<<这里规模不大,1秒都不到
Before choose issue clock() returns: 13687813 clocks-per-sec (13.69 sec)
times() yields: user CPU:13.60; system CPU:0.08


Segmentation fault<<<<<<<这里因为规模太大,递归过头,段错误。N = 1 0000 0000;

root@ltegan:chapter1# 

//代码:

/*
* choose issue, there are N numbers, ensure the k-th max number
* 3 4 2 5, 3-th max number is 3
* sort the numbers big -> small, and return index k number
*/
#include "stdio.h"
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <sys/times.h>
#include <unistd.h>

int find_mid(unsigned int * array, int start, int end)
{
    int mid = start;
    unsigned int tmp;
    int i;
    for (i = start+1; i <= end; i++) {
        if (array[mid] < array[i]) {
            tmp = array[mid];
            array[mid] = array[i];
            if (mid+1 == i) {
                array[i] = tmp;
            }
            else {
                array[i] = array[mid+1];
                array[mid+1] = tmp;
            } 
            mid++;
        }
   }
    return mid;
}


int my_qsort(unsigned int * array, int start, int end)
{
    if (start < end) {
        int mid = find_mid(array, start, end);
        my_qsort(array, start, mid-1);
        my_qsort(array, mid+1, end);
    }
    return 0;
}

static clock_t 
displayProcessTimes(const char *msg)
{
    struct tms t;
    clock_t clockTime; 

    static long clockTicks = 0;

    if (msg != NULL) printf("%s", msg);
    if (clockTicks == 0) {
        clockTicks = sysconf(_SC_CLK_TCK);
        if (clockTicks == -1) exit(-1); 
    }
    clockTime = clock();
    if (clockTime == -1) exit(-1);
    printf(" clock() returns: %ld clocks-per-sec (%.2f sec)\n",
            (long)clockTime, (double)clockTime / CLOCKS_PER_SEC);

        //打印一下cpu用时和用户进程用时。
    if (times(&t) == -1) exit(-1);
    printf(" times() yields: user CPU:%.2f; system CPU:%.2f\n",
        (double)t.tms_utime/clockTicks, (double)t.tms_stime/clockTicks);
    return clockTime; 
}


void print_array(unsigned int * array, int a_len)
{
    size_t i;
    printf("========================\n");
    for (i = 0; i < a_len; i++ ) {
        printf(" %u", array[i]);
    }
    printf("\n########################\n");
} 
unsigned int choose_issue(unsigned int * array, int a_len, int k)
{
    //print_array(array, a_len);
    my_qsort(array, 0, a_len-1);
    //print_array(array, a_len);

    return array[k-1];
}


unsigned int * full_array( int n )
{
    int a_len, i;
    unsigned int * array = NULL;
    a_len = n * sizeof(unsigned int);
    array = malloc(a_len);
    if (array) {
        memset(array, 0, a_len);
        srand(time(NULL));
        for (i = 0; i < n; i++) {
               array[i] = rand() % 100;
        }
    }
    else {
        assert(0);
    }

    return array;
}


int main()
{
    int N = 0;
    clock_t startClock, stopClock;
    unsigned int *array = NULL;
    N = 1000000;
    array = full_array( N );
    startClock = displayProcessTimes("Before choose issue"); 
    printf("Your K[%d] max is :%u \n", N/2, choose_issue(array, N, N/2));
                stopClock = displayProcessTimes("After choose issue"); 
    printf("N:%d, clock:%ld(%.2f sec)\n", N, (long)(stopClock - startClock), 
                (double)(stopClock - startClock)/CLOCKS_PER_SEC);
    if (array) free(array);


    N = 1000;
    array = full_array( N );
    startClock = displayProcessTimes("Before choose issue"); 
    printf("Your K[%d] max is :%u \n", N/2, choose_issue(array, N, N/2));
    stopClock = displayProcessTimes("After choose issue"); 
    printf("N:%d, clock:%ld(%.2f sec)\n", N, (long)(stopClock - startClock), 
            (double)(stopClock - startClock)/CLOCKS_PER_SEC);
    if (array) free(array);


    N = 100000000;
    array = full_array( N );
    startClock = displayProcessTimes("Before choose issue"); 
    printf("Your K[%d] max is :%u \n", N/2, choose_issue(array, N, N/2));
        stopClock = displayProcessTimes("After choose issue"); 
    printf("N:%d, clock:%ld(%.2f sec)\n", N, (long)(stopClock - startClock), 
        (double)(stopClock - startClock)/CLOCKS_PER_SEC);
    if (array) free(array);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值