C語言使用鏈表的插入排序、選擇排序、快速排序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>

typedef int keytype;
#define MAXSIZE 20
clock_t clock( void ); 
typedef struct
{
   keytype key;//某個元素的一些特徵,可以還有其他的
}elemtype;


typedef struct
{
   elemtype elem[MAXSIZE];
   int length;
}SStable;//順序表的結構


void Creatlist(SStable *LIST)//創建線性表,注意此處的LIST是指針類型,爲了使返回這個線性表
{
   int i;
   printf("Please input how many the number is:\n");
   scanf("%d",    &LIST->length);//輸入線性表的長度
   printf("Please input the  count of %d number\n",LIST->length);
   for(i=1;i<=LIST->length;i++)
   {
      scanf("%d",&LIST->elem[i].key);//注意此處的爲LIST->elem而不是LIST.elem
   
   }
   printf("\n");

}
/*注意當LIST爲指針類型的時候在調用起內部參數的時候要用->
LIST不爲指針類型的時候用  .  */

void SHowLIST(SStable  LIST)//顯示所構造的線性表內部的數據
{
   int i;
   printf("The number is:\n");
   for(i=1;i<=LIST.length;i++)
   printf("%5d",LIST.elem[i].key);
   printf("\n");
}
void InsertSort(SStable L)//插入排序
{
   int i,j;
   for(i=2;i<=L.length;i++)
      if(L.elem[i].key<L.elem[i-1].key)//L.elem[i]插入到有序的子表中
      {
         L.elem[0]=L.elem[i];//複製爲哨兵
         for(j=i-1;L.elem[0].key<L.elem[j].key;j--)
         L.elem[j+1]=L.elem[j];//記錄後移
         L.elem[j+1]=L.elem[0];//插入到正確的位置,
                          /*注意此處爲什麼是J+1,那是
         因爲上面的循環後j的值減了一。*/
      }
      SHowLIST(L);//顯示數據
}
/*插入排序的思想是:
i=2;開始若後面的那個元素的值小於前面的元素的值,則將那個元素複製給哨兵
再從末尾往後移一位,並將元素插入到正確的位置,
*/


int Search_SEQ(SStable ST,keytype key)//查找與自己想要找的數據的位置
{
   int i;
   ST.elem[0].key=key;//“哨兵   for(i=ST.length;ST.elem[i].key!=key;--i);/*注意此處最後有一個分號
   ,作用是到了某個地點i,就找到了匹配的那個地點了*/
      return i;//返回位置

}
int SelectMaxkey(SStable L,int i)//用選擇排序輸出最大的值得位置
{
   int j,temp,p;
   p=i;
   for(j=i+1;j<=L.length;j++)
   {
      if(L.elem[p].key<L.elem[j].key)
      {  p=j;
      }
   }
   return p;
}
void Select(SStable &L)//選擇排序,從大到小排序
{
   int i,j,temp;
   for(i=1;i<L.length;i++)
   {
      j=SelectMaxkey(L,i);//尋找最大的座標
         if(i!=j)
         {
         L.elem[0]=L.elem[j];
         L.elem[j]=L.elem[i];
         L.elem[i]=L.elem[0];
         }
      }
}
int Partition(SStable &L,int low,int high)//快速排序將數字序列分成兩部分
{
   int pviotkey;
   L.elem[0]=L.elem[low];//將第一個數字的位置給哨兵
   pviotkey=L.elem[low].key;//將第一個數字設置爲中軸數字
   while(low<high)
   {
      while(low<high&&(L.elem[high].key>=pviotkey))
         --high;/*注意此部分是若從最後面的數字要大於中軸數字,
high--,從而找到比中軸數字小的數字*/
      L.elem[low]=L.elem[high];//將後面比中軸數字小的數字位置移至前面
      while(low<high&&L.elem[low].key<=pviotkey)
         ++low;/*注意此部分是若從最前面的數字要小於中軸數字,
low++,從而找到比中軸數字大的數字*/
         L.elem[high]=L.elem[low];//將比前面比中軸數字大的位置移至前面
   }
      L.elem[low]=L.elem[0];
return low;//返回中軸數字的位置
}


void Qsort(SStable &L,int low,int high)//快速排序
{  
   int part;
   if(low<high)
   {
      part=Partition(L,low,high);//將這串數字以中軸數字分成兩部分
      Qsort(L,low,part-1);//對中軸線左邊的部分進行快速排序
      Qsort(L,part+1,high);//對中軸線右邊的部分進行快速排序
   }
   
}
void QsortFound(SStable L)//快速排序
{
   Qsort(L,1,L.length);
   SHowLIST(L);
}
long NOWTime()//時間函數
{
   long now;
   now=clock();
   return now;
}

void main()
{  
   SStable Found;//注意類型
   int number,i,n,function;
long now,end,now1,end1;

begin:
   for(i=0;i<=25;i++)
      printf("==");
      printf("\n");
   printf("1.構造線性表。\n");
   printf("2.顯示出線性表!\n");
   printf("3.查找相應數字在線性表中的位置。\n");
   printf("4.快速排序!\n");
   printf("5.插入排序!\n");
   printf("6.選擇排序!\n");
      for(i=0;i<=25;i++)
      printf("==");
      printf("\n");
   printf("請輸入相應操作:");
   scanf("%d",&n);
   switch(n)
   {
   case 1:Creatlist(&Found);
      Sleep(2000);
      system("cls");
      goto begin;
      
   case 2:SHowLIST(Found);
      Sleep(2000);
      system("cls");
      goto begin;
   case 3:
   printf("Please input the Found  of  number is:\n");
   scanf("%d",&number);

   function=Search_SEQ(Found,number);
   printf("The number of %d' location is\n",number);
   printf("%d",function);
   printf("\n");
   Sleep(2000);
      system("cls");
   goto begin;
   case 4:
      now=NOWTime();
      printf("快速查找的結果爲:\n");
      QsortFound(Found);//快速排序
      
      end=NOWTime();
      printf("快速排序所用的時間爲:%fs\n",(double)(end-now)/CLOCKS_PER_SEC);
      Sleep(2000);
      system("cls");
      goto begin;
   case 5:InsertSort(Found);//插入排序
      Sleep(2000);
      system("cls");
      goto begin;
   case 6:
      now1=NOWTime();
      printf("選擇排序的結果爲:\n");
      Select(Found);
      SHowLIST(Found);
      end1=NOWTime();
      printf("選擇排序所用的時間爲:%fs\n",(double)(end1-now1)/CLOCKS_PER_SEC);
      Sleep(2000);
      system("cls");
      goto begin;
   
   case 0:
      goto end;break;
   default:printf("輸入錯誤!\n");
   }
end:printf("謝謝使用!\n");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章