Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

话说《算法导论》上面的排序算法实在是经典……

Posted by flyfy1 at 2009-09-14 13:33:52 on Problem 1007
昨晚刚刚看完第一章介绍的排序算法,结果这里就用上了……回忆的写出来,越写越觉得那个算法的精妙……好书啊!

顺便贴出来源程序……

/*  algorithms:
get the input: int length,num;
string a[100][51];

for culculation: int count[num.];
for(i=0;i<length[])
    for: j- 0~length[]-1{
        for: t - (j+1~length)
            if(a[i][j]>a[i][t]) count[i]++;
            
    (order of the output) order[num.]:
initialize:
    for i:0~(num-1): order[i]=i;
sortting:
    for(i=1;i<num;i++){
        if(count[i-1]>count[i]){
            tem=count[i];temo=order[i];
            for(j=i;count[j-1]>tem && j>0;j--){
                count[j]=count[j-1];order[j]=order[j-1];
            }
            count[j]=tem;order[j]=temo;
        }
    }
            
            output: for(i=0;i<num;i++) printf("%s",string[100][order[num.]]);            
            
End of Algorithms*/

#include <stdio.h>
main(){
    int length,num,order[100],count[100],tem,temo,i,j,t; char a[100][51];
    scanf("%d%d",&length,&num);
    for(i=0;i<num;i++) scanf("%s",a[i]);
    for(i=0;i<num;i++) count[i]=0;
    for(i=0;i<num;i++)
        for(j=0;j<length;j++)
            for(t=j+1;t<length;t++)
                if(a[i][j]>a[i][t]) count[i]++;


    for(i=0;i<num;i++) order[i]=i;
    for(i=1;i<num;i++){
        if(count[i-1]>count[i]){
            tem=count[i];temo=order[i];
            for(j=i;count[j-1]>tem && j>0;j--){
                count[j]=count[j-1];order[j]=order[j-1];
            }
            count[j]=tem;order[j]=temo;
        }
    }
    for(i=0;i<num;i++) printf("%s\n",a[order[i]]);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator