排序算法比较程序

时间:2010-04-30 11:52:12  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:>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 EXIT"};  clrscr();  printf(...
  正文:功能要求如下:

排序算法比较: shellsort, quicksort, heaphotoshop/ target=_blank class=infotextkey>psort, 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 EXIT"};

  clrscr();
  printf("SORT COMPARE SYSTEMn");
  for (i=0;i<6;i++) printf("%sn",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("SORTn");
  for(i=0;i<5;i++) printf("%sn",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 RESULTn");
  for(i=0;i<4;i++) printf("%sn",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("%sn",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,"%dn",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++;
        if(L.key[j]>L.key[j+gap])
        {
          compCount++;
          x=L.key[j];shiftCount++;
排序算法比较程序》由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/VC/2010-04-30/13822.html

 1/4    1 2 3 4 下一页 尾页


关键字:

关于《排序算法比较程序》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td