第十六周專案1 驗證演算法

2021-07-25 09:36:06 字數 3628 閱讀 3620

問題:

/*  

* 檔名稱:專案1-3.cbp

* 作 者:秦緒龍

* 完成日期:2023年12月15日

* 版 本 號:v1.0

* 問題描述:驗證氣泡排序

* 輸入描述:無

* 程式輸出:測試資料

*/

1.直接插入排序
#include #define maxsize 20

typedef int keytype; //定義關鍵字型別

typedef char infotype[10];

typedef struct //記錄型別

rectype; //排序的記錄型別定義

void insertsort(rectype r,int n) //對r[0..n-1]按遞增有序進行直接插入排序

; for (i=0; i執行結果:

2..折半插入排序

**:#include #define maxsize 20

typedef int keytype; //定義關鍵字型別

typedef char infotype[10];

typedef struct //記錄型別

rectype; //排序的記錄型別定義

void insertsort1(rectype r,int n) //對r[0..n-1]按遞增有序進行直接插入排序

}int main()

; for (i=0; i

執行結果:

3.希爾排序

**:#include #define maxsize 20

typedef int keytype; //定義關鍵字型別

typedef char infotype[10];

typedef struct //記錄型別

rectype; //排序的記錄型別定義

void shellsort(rectype r,int n) //希爾排序演算法

rectype; //排序的記錄型別定義

void bubblesort1(rectype r,int n)

rectype; //排序的記錄型別定義

void quicksort(rectype r,int s,int t) //對r[s]至r[t]的元素進行快速排序

rectype; //排序的記錄型別定義

void quicksort1(rectype r,int s,int t) //對r[s]至r[t]的元素進行快速排序

rectype; //排序的記錄型別定義

//調整堆

void sift(rectype r,int low,int high)

}int main()

;//a[0]空閒,不作為關鍵字

for (i=1; i<=n; i++)

r[i].key=a[i];

printf("排序前:");

for (i=1; i<=n; i++)

printf("%d ",r[i].key);

printf("\n");

heapsort(r,n);

printf("排序後:");

for (i=1; i<=n; i++)

printf("%d ",r[i].key);

printf("\n");

return 0;

}

執行結果:

7.歸併排序
**:
#include #include #define maxsize 20

typedef int keytype; //定義關鍵字型別

typedef char infotype[10];

typedef struct //記錄型別

rectype; //排序的記錄型別定義

void merge(rectype r,int low,int mid,int high)

else //將第2段中的記錄放入r1中

while (i<=mid) //將第1段餘下部分複製到r1

while (j<=high) //將第2段餘下部分複製到r1

for (k=0,i=low; i<=high; k++,i++) //將r1複製回r中

r[i]=r1[k];

}void mergepass(rectype r,int length,int n) //對整個數序進行一趟歸併

rectype;

void crealink(rectype *&p,char *a,int n);

void displink(rectype *p);

void radixsort(rectype *&p,int r,int d) //實現基數排序:*p為待排序序列鍊錶指標,r為基數,d為關鍵字位數

else

p=p->next; //取下乙個待排序的元素

}p=null; //重新用p來收集所有結點

for (j=0; jnext=head[j];

t=tail[j];}}

t->next=null; //最後乙個結點的next域置null

//以下的顯示並非必要

printf(" 按%d位排序\t",i);

displink(p);

}}void createlink(rectype *&p,char a[maxe][maxd],int n) //採用後插法產生鍊錶

else

}t->next=null;

}void displink(rectype *p) //輸出鍊錶

printf("\n");

}int main()

; for (i=0; i執行結果:

第十六周 專案1 驗證演算法

檔名稱 main.cpp,btree.h,btree.cpp 完成日期 2015年12月14日 版本號 code block 12.11 問題描述 程式,觀察執行結果並深刻領會演算法的思路和實現方法 2 希爾排序 測試 include define maxsize 20 typedef int ke...

第十六周 專案1 驗證演算法

檔名稱 main.cpp,btree.h,btree.cpp 完成日期 2015年12月14日 版本號 code block 12.11 問題描述 程式,觀察執行結果並深刻領會演算法的思路和實現方法 4 快速排序 測試 include define maxsize 20 typedef int ke...

第十六周 專案1 驗證演算法

檔名稱 main.cpp,btree.h,btree.cpp 完成日期 2015年12月14日 版本號 code block 12.11 問題描述 程式,觀察執行結果並深刻領會演算法的思路和實現方法 歸併排序 測試 include include define maxsize 20 typedef ...