日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构实验四 排序算法的实现

發布時間:2023/12/10 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构实验四 排序算法的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

廣州大學學生實驗報告

?

開課實驗室:計算機科學與工程實驗(電子樓416) ????2019年6月4日

學院

計算機科學與教育軟件學院

年級、專業、班

?

姓名

?

學號

?

實驗課程名稱

數據結構實驗

成績

?

實驗項目名稱

實驗四 排序算法

指導老師

?????

一、實驗目的

掌握線性的定義及基本操作,用鏈表實現:遍歷、查找、插入、刪除、翻轉。

二、使用儀器、器材

微機一臺

操作系統:WinXP

編程軟件:C++

三、實驗內容及原理

用隨機函數生成16個2位正整數(10~99),從復雜度為O(n2) 的插入排序、選擇排序、冒泡(雙向冒泡)和復雜度為O(nlog2n) 的堆排序、快速排序、歸并排序等多種排序算法各選1種實現,輸出排序中間過程、統計關鍵字的比較次數和記錄的移動次數。

?

  • 復雜度O(n2)的排序算法選用的是直接插入排序
  • 復雜度O(nlog2n)的排序算法選用的是堆排序
    • 實驗過程原始數據記錄

    直接插入排序

    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.堆排序

    ?

    ?

    ?

    ?

    ?????????

    ?

    總結

    以上是生活随笔為你收集整理的数据结构实验四 排序算法的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。