第十六周專案1

2021-07-25 13:17:27 字數 2648 閱讀 4889

檔名稱:1.cpp 

完成日期:2023年9月12日 

版本號:v1.0 

**(1)氣泡排序

#include #define maxsize 20  

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

typedef char infotype[10];

typedef struct //記錄型別

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

void bubblesort(rectype r,int n)

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

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

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

void selectsort(rectype r,int n)

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;

}

執行結果:

(5)歸併排序

#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)

計算機控制工程學院 班級 計156 2 姓名 陳飛 問題及 問題描述 插入排序之直接插入排序 輸入描述 無 程式輸出 見執行結果 include define maxsize 20 typedef int keytype 定義關鍵字型別 typedef char infotype 10 typede...

第十六周專案1 氣泡排序

檔名稱 main.cpp 完成日期 2015.12.14 版本號 v1.0 問題描述 給定序列,採用氣泡排序的演算法,描述排序的過程 include define maxsize 20 typedef int keytype 定義關鍵字型別 typedef char infotype 10 type...

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

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