首页 > 学院 > 开发设计 > 正文

排序算法比较程序

2019-11-17 05:33:47
字体:
来源:转载
供稿:网友

  功能要求如下:

排序算法比较: shellsort, quicksort, heapsort, mergesort 的算法实现 ,
对同样数据集的排序时间比较。


源代码:

# include <stdio.h>
# include <time.h>

# define MAXSIZE 2000

typedef strUCt{
    int key[MAXSIZE];
    int length;
}list;


long int  compCount;
long int  shiftCount;


void menu(int *m)/*retun m*/
{
    int i;
    char menu[6][15]={"1 CREATE ","2 IMPORT ","3 SORT","4 SHOW RESULT",
                           "5 SAVE RESULT","6 EX99v"};

    clrscr();
    PRintf("SORT COMPARE SYSTEM/n");
    for (i=0;i<6;i++) printf("%s/n",menu[i]);
    printf("/n Please Select (1-6):/n");
    
    scanf("%d",m);

}



void menusort(int *m)/*retun m*/
{
    int i;
    char menusort[5][15]={"1 SHELL SORT","2 QUICK SORT","3 HEAP SORT",
                            "4 MERGE SORT","5 ALL SORT"};
    
    clrscr();
    printf("SORT/n");
    for(i=0;i<5;i++) printf("%s/n",menusort[i]);
    printf("/n Please Select (1-5):/n");
    
    scanf("%d",m);

}


void menushow(int *m)/*retun m*/
{
    int i;
    char menushow[4][20]={"1 SHELL SORT RESULT","2 QUICK SORT RESULT",
                            "3 HEAP SORT RESULT","4 MERGE SORT RESULT"};
    
    clrscr();
    printf("SHOW SORT RESULT/n");

    for(i=0;i<4;i++) printf("%s/n",menushow[i]);
    printf("/n Please Select (1-4):/n");
    
    scanf("%d",m);

}

void menusave(int *m)
{
    int i;
    char menusave[4][20]={"1 SHELL SORT RESULT","2 QUICK SORT RESULT",
                           "3 HEAP SORT RESULT","4 MERGE SORT RESULT"};
    
    clrscr();
    printf("SAVE:/n");
    for (i=0;i<4;i++) printf("%s/n",menusave[i]);
    printf("/n Please Select (1-4):/n");
    
    scanf("%d",m);
}

void create(list *L)
{
    int i;
    
    printf("HOW MANY DATA?/n");
    scanf("%d",&((*L).length));
    
    for(i=1;i<=(*L).length;i++)
    {
        printf("/nPLEASE INPUT THE %dth DATA:/n",i);
        scanf("%d",&(*L).key[i]);
    }
    printf("/nCREATE COMPLETE !/n");
        
}


int listopen(list *L,char *filename)
{
    int k=1;
    FILE *data;
    
    data=NULL;


    data=fopen(filename,"rb");
    
    while (! feof(data))
        {
            fscanf(data,"%d",&(*L).key[k]);
            k++;
        }
        (*L).length=k-1;
}

void import(list *L)/*fix L*/
{
    char filename[255];
    int i;

    printf("/nPLEASE INPUT THE FILE PATH AND NAME:/n");

    scanf("%s",filename);

    clrscr();
    listopen(L,filename);
    for(i=1;i<(*L).length;i++) printf("%d ",(*L).key[i]);
    printf("/nPRESS ANYKEY RETURN TO MAINMENU.../n");
    getch();
}

void save(list L)
{
    FILE *data;
    char filename[255];
    int r;

    printf("/nPLEASE INPUT THE FILE PATH AND NAME:/n");
    scanf("%s",filename);

    data=fopen(filename,"wb");
    for(r=1;r<=L.length;r++) fprintf(data,"%d/n",L.key[r]);
    fclose(data);
    printf("SAVE OK! /n PRESS ANY KEY TO RETURN THE MAINMENU... ");
    getch();
        
}


list shellsort(list L)/*retun L_SHELL*/
{
    int i,j,gap,x,n;
    
    compCount=shiftCount=0;
    n=L.length;
    gap=n/2;
    while (gap>0)
    {
        compCount++;
        for(i=gap+1;i<=n;i++)
        {
            compCount++;
            j=i-gap;
            while(j>0)
            {
                compCount++;

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表