数据结构实验四 排序算法的实现
廣州大學學生實驗報告
?
開課實驗室:計算機科學與工程實驗(電子樓416) ????2019年6月4日
| 學院 | 計算機科學與教育軟件學院 | 年級、專業、班 | ? | 姓名 | ? | 學號 | ? | |
| 實驗課程名稱 | 數據結構實驗 | 成績 | ? | |||||
| 實驗項目名稱 | 實驗四 排序算法 | 指導老師 | ????? | |||||
| 一、實驗目的 掌握線性的定義及基本操作,用鏈表實現:遍歷、查找、插入、刪除、翻轉。 二、使用儀器、器材 微機一臺 操作系統:WinXP 編程軟件:C++ 三、實驗內容及原理 用隨機函數生成16個2位正整數(10~99),從復雜度為O(n2) 的插入排序、選擇排序、冒泡(雙向冒泡)和復雜度為O(nlog2n) 的堆排序、快速排序、歸并排序等多種排序算法各選1種實現,輸出排序中間過程、統計關鍵字的比較次數和記錄的移動次數。 ?
直接插入排序 Main.cpp #include "stdafx.h" #include"InsertSort.h" ? int main() { ??? int n = 10; ??? RecType R[MAXL]; ??? KeyType a[] = { 9,8,7,6,5,4,3,2,1,0 }; ??? CreateList(R, a, n); ??? printf("排序前:"); DispList(R, n); ??? int compare, move; ??? InsertSort(R, n,compare,move); ??? printf("排序后:"); DispList(R, n); ??? printf("\n"); ??? printf("關鍵字比較次數:%d \n",compare); ??? printf("記錄的移動次數:%d ",move); ??? return 1; } InsertSort.h ? ? //順序表基本運算算法 #include <stdio.h> #define MAXL 100????? //最大長度 typedef int KeyType;? //定義關鍵字類型為int typedef char InfoType; ? typedef struct { ??? KeyType key;????? //關鍵字項 ??? InfoType data;??????? //其他數據項,類型為InfoType } RecType;?? ???????????? //查找元素的類型 ? void swap(RecType x, RecType y);?? //x和y交換 void CreateList(RecType R[], KeyType keys[], int n);//創建順序表 void DispList(RecType R[], int n); //輸出順序表 //-------------------------------------------------------------------直接插入排序算法 void InsertSort(RecType R[], int n, int& compare, int& move); //對R[0..n-1]按遞增有序進行直接插入排序 InsertSort.cpp ? #include"stdafx.h" #include"InsertSort.h" void swap(RecType x, RecType y)??? //x和y交換 { ??? RecType tmp = x; ??? x = y; y = tmp; } ? void CreateList(RecType R[], KeyType keys[], int n)? //創建順序表 { ??? for (int i = 0; i<n; i++)????????? //R[0..n-1]存放排序記錄 ???????? R[i].key = keys[i]; } void DispList(RecType R[], int n)? //輸出順序表 { ??? for (int i = 0; i<n; i++) ???????? printf("%d ", R[i].key); ??? printf("\n"); } //-------------------------------------------------------------------直接插入排序算法 void InsertSort(RecType R[], int n, int& compare, int& move) //對R[0..n-1]按遞增有序進行直接插入排序 { ??? int i, j; RecType tmp; ??? compare=0; ??? move = 0; ??? for (i = 1; i<n; i++) ??? { ???????? if (R[i].key<R[i - 1].key) //反序時 ???????? { ???????????? compare++; ???????????? tmp = R[i]; ???????????? j = i - 1; ???????????? do??????????????????? //找R[i]的插入位置 ???????????? { ????????????????? R[j + 1] = R[j];?? ?? //將關鍵字大于R[i].key的記錄后移 ????????????????? j--; ????????????????? move++; ???????????? } while (j >= 0 && R[j].key>tmp.key); ???????????? R[j + 1] = tmp;????? ????? //在j+1處插入R[i] ???????? } ???????? printf("? i=%d: ", i); DispList(R, n); ??? } } 堆排序 Main.cpp ? //堆排序算法 #include"stdafx.h" #include "InsertSort.h" ? ? int main() { ??? int n = 10; ??? int compare = 0; ??? int move = 0; ??? RecType R[MAXL]; ??? KeyType a[] = { 15,18,29,12,35,32,27,23,10,20 }; ??? CreateList1(R, a, n); ??? printf("排序前:"); DispList1(R, n); ??? HeapSort(R, n,compare,move); ??? printf("排序后:"); DispList1(R, n); ??? printf("關鍵字比較次數:%d \n",compare); ??? printf("記錄移動次數:%d \n",move); ??? return 1; } ? HeapSort.h ? ? //順序表基本運算算法 #include <stdio.h> #define MAXL 100????? //最大長度 typedef int KeyType;? //定義關鍵字類型為int typedef char InfoType; ? typedef struct { ??? KeyType key;????? //關鍵字項 ??? InfoType data;??????? //其他數據項,類型為InfoType } RecType;??????????????? //查找元素的類型 ? ????????????????????????? //----以下運算針對堆排序的程序 void CreateList1(RecType R[], KeyType keys[], int n); //創建順序表 void DispList1(RecType R[], int n); //輸出順序表 //-------------------------------------------------------------------直接插入排序算法 void sift(RecType R[], int low, int high, int&compare, int& move); void HeapSort(RecType R[], int n, int &compare, int &move); ? HeapSort.h ? #include"stdafx.h" #include"HeapSort.h" void CreateList1(RecType R[], KeyType keys[], int n) //創建順序表 { ??? for (int i = 1; i <= n; i++)??????????? //R[1..n]存放排序記錄 ???????? R[i].key = keys[i - 1]; } void DispList1(RecType R[], int n) //輸出順序表 { ??? for (int i = 1; i <= n; i++) ???????? printf("%d ", R[i].key); ??? printf("\n"); } //-------------------------------------------------------------------直接插入排序算法 void sift(RecType R[], int low, int high,int&compare,int& move) { ??? int i = low, j = 2 * i;???? ??????????????????? //R[j]是R[i]的左孩子 ??? RecType temp = R[i]; ??? while (j <= high) ??? { ???????? if (j < high && R[j].key < R[j + 1].key) ?? //若右孩子較大,把j指向右孩子 ???????? { ???????????? j++; compare++; ???????? }?? ????????????????????? //變為2i+1 ???????? if (temp.key<R[j].key) ???????? { ???????????? R[i] = R[j];????????????? ???? //將R[j]調整到雙親結點位置上 ???????????? i = j;??????????????????? ???? //修改i和j值,以便繼續向下篩選 ???????????? j = 2 * i; ???????????? compare++; ???????????? move++; ???????? } ???????? else break;???????????????? ?????? //篩選結束 ??? } ??? R[i] = temp;//被篩選結點的值放入最終位置 } ? void HeapSort(RecType R[], int n,int &compare,int &move) { ??? compare = 0; ??? move = 0; ??? int i; ??? RecType tmp; ??? for (i = n / 2; i >= 1; i--)?? //循環建立初始堆,調用sift算法 n/2 次 ???????? sift(R, i, n, compare,move); ??? printf("初始堆:"); DispList1(R, n); ??? for (i = n; i >= 2; i--)?????? //進行n-1趟完成推排序,每一趟堆排序的元素個數減1 ??? { ???????? tmp = R[1];?????????? //將最后一個元素與根R[1]交換 ???????? R[1] = R[i]; ???????? R[i] = tmp; ???????? move+=3; ???????? printf("第%d趟: ", n - i + 1); DispList1(R, n); ???????? sift(R, 1, i - 1, compare,move);??????? //對R[1..i-1]進行篩選,得到i-1個節點的堆 ???????? printf("篩選為:"); DispList1(R, n); ??? } } ? 五、實驗結果及分析 1.直接插入排序 ? ? 2.堆排序 ? ? | ||||||||
| ? ? | ||||||||
| ? | ? | ? | ? | ? | ? | ? | ? | ? |
?
總結
以上是生活随笔為你收集整理的数据结构实验四 排序算法的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 6、MDT组件安装
- 下一篇: 深入理解JAVA虚拟机大全