【C語言】選擇排序算法-習題

#include <stdio.h>
#include <stdlib.h>
/*C語言程序設計案例教程(廖湖聲) P122 第四章 上機練習 第一題*/
/*北工大896 計算機*/

/*選擇排序*/
int select(int num[],int count[]){
    int i,j,max,temp_num,temp_count;
    for(i=0;i<99;i++){
        max=i;
        for(j=i+1;j<100;j++){
            if(count[j]>count[max]){
                max=j;
            }
        }
        if(max!=i){
            /*交換num*/
            temp_num=num[i];
            num[i]=num[max];
            num[max]=temp_num;
            /*交換count*/
            temp_count=count[i];
            count[i]=count[max];
            count[max]=temp_count;
        }
    }
}

int main()
{
    int num[100];
    int count[100];
    int i;

    /*生成待排序列*/
    for(i=0;i<100;i++){
        num[i]=i+1;
        count[i]=rand()%100;
    }
    select(num,count);//選擇排序函數
    /*打印結果*/
    printf("Top 10(order by download times):\n");
    for(i=0;i<10;i++){
        printf("number:%d,download times:%d\n",num[i],count[i]);
    }
    printf("\n");
    printf("Never been download:\n");
    for(i=0;i<100;i++){
        if(count[i]==0)
        printf("number:%d,download times:%d\n",num[i],count[i]);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章