zoukankan      html  css  js  c++  java
  • 利用快速排序原理找出数组中前n大的数

    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    #define MAX_SIZE 400001
    // 生成不重复的随机数序列写入文件
    void gen_test_data(uint32_t cnt)
    {
        if( cnt >= MAX_SIZE){printf("cnt too largr
    ");return;}
        uint32_t i = 0;
        char buf[MAX_SIZE];
        for(;i < cnt;++i){buf[i] = 1;}
        uint32_t n = 0;
        char file_name[256];
        snprintf(file_name,256,"test_data_%d.txt",cnt);
        FILE *fp = fopen(file_name,"w");
        if(NULL == fp){printf("open %s error!
    ",file_name);return;}
        while(n < cnt)
        {
            int32_t nRand = rand() % cnt;
            while(buf[nRand] == 0)nRand = (nRand + 1)%cnt;
            buf[nRand] = 0;
            fprintf(fp,"%d ",nRand);
            ++n;
        }
        fclose(fp);
    }
    
    // 读取文件
    void read_data(int32_t arr[],const uint32_t size,uint32_t *cnt,const int32_t data_cnt)
    {
        FILE *fp = NULL;
        char file_name[256];
        if(data_cnt > size){printf("data_cnt too largr
    ");return;}
        snprintf(file_name,256,"test_data_%d.txt",data_cnt);
        fp = fopen(file_name,"r");
        if(NULL == fp){printf("open %s error!
    ",file_name);return;}
        while(!feof(fp) && *cnt < size)
        {
            fscanf(fp,"%d ",&arr[*cnt]);
            (*cnt)++;
        }
        fclose(fp);
    }
    
    // 快速排序
    void quick_sort(int32_t arr[],int32_t low,int32_t high)
    {
        if(low >= high)return;
        int32_t i = low,j = high,tmp = arr[i];
        while(i<j)
        {
            while(i<j && arr[j] <= tmp)j--;
            if(i<j){arr[i] = arr[j];i++;}
            while(i<j && arr[i] > tmp)i++;
            if(i<j){arr[j] = arr[i];j--;}
        }
        arr[i] = tmp;
        quick_sort(arr,low,i-1);
        quick_sort(arr,i+1,high);
    }
    
    // 找出最大n个数
    void get_topn(int32_t arr[],int32_t low,int32_t high,const int32_t topn)
    {
        if(low >= high || topn > high)return;
        int32_t i = low,j = high,tmp = arr[i];
        while(i<j)
        {
            while(i<j && arr[j] < tmp)j--;
            if(i<j)arr[i++] = arr[j];
            while(i<j && arr[i] >= tmp)i++;
            if(i<j)arr[j--] = arr[i];
        }
        arr[i] = tmp;
        int32_t n = i - low + 1;
        if (n == topn)return;
        else if (n > topn)
            get_topn(arr, low, i-1, topn);
        else if (n < topn)
            get_topn(arr, i+1, high, topn - n);
    }
    
    void dump(int32_t arr[],const uint32_t cnt)
    {
        uint32_t i = 0;
        for(;i < cnt;++i)
        {
            printf("%6d ",arr[i]);
        }
        printf("
    ");
    }
    
    int32_t main(int32_t argc,char *argv[])
    {
        int32_t data_cnt = 40000,top = 500;
        int32_t arr[MAX_SIZE];
        uint32_t cnt = 0;
        gen_test_data(data_cnt);
        read_data(arr,MAX_SIZE,&cnt,data_cnt);
        get_topn(arr,0,cnt-1,top);
        quick_sort(arr,0,top-1);
        dump(arr,top);
        //quick_sort(arr,0,cnt);
        //dump(arr,cnt);
        return 0;
    }
  • 相关阅读:
    ARMV7-M数据手册---Part A :Application Level Architecture---A7 Instruction Details
    卷积神经网络
    Theano深度学习结构分析
    BP神经网络
    Softmax回归
    CART:分类与回归树
    基于单决策树的AdaBoost
    Logistic回归
    Qt搭建多线程Server
    支持向量机(SVM)
  • 原文地址:https://www.cnblogs.com/tangxin-blog/p/5617736.html
Copyright © 2011-2022 走看看