对这100万个整数进行排序(每个数是1--100间)

    技术2022-05-11  128

    *已知函数sort的参数iArray是一个指向100万个在存储空间上连续的整数的指针,这些数的范围从1到100请写出对这100万个整数进行排序并显示出来的算法函数原型假定为:void sort(int*iArray)   //iArray为指向100万个整数的指针

    题目来自csdn c板块

    原地址:http://community.csdn.net/Expert/topic/4473/4473425.xml?temp=.44475952005-12-21 marsarden*/#include <stdio.h>#include <stdlib.h> //for srand and rand#include <time.h>   //for time#define MAX_L 1000000void sort(int *iArray){    int final[100];    int index,i,j,k;

        for(i=0;i<100;i++) final[i]=0; //we should set 0 to final    // traverse    for(i=0;i<MAX_L;++i)    {        index=iArray[i];        if(index<=100)        final[index]++;

        }//for

        for(j=0;j<100;++j)    {

        if(final[j]>0)        {          //你可以用for都打印出来:         //for(k=0;k<final[j];k++)         //printf("%d",j);         //这里就统计个数了 :         printf("have num %d : %d/n",j,final[j]);        }

        }//for}

    //for test sortint main(void){int testarr[MAX_L];int n;srand ( time(NULL) );for(n=0;n<MAX_L;n++)    {    testarr[n]=rand()1; //to get a rand int from 0 to 100    }

    sort(testarr);         getchar();return 0;

    }


    最新回复(0)