zoukankan      html  css  js  c++  java
  • 数据结构 实验六 排序

    数据结构 实验六  排序

    本次实验实现排序中的直接插入、冒泡排序、快速排序、简单选择排序、堆排序等排序算法。

    下面放一下自己的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include "DataHead.h"
    
    #define MAXSIZE 20
    
    typedef int KeyType;
    
    typedef struct {
        KeyType key;
        //InfoType otherinfo;
    }RedType;
    
    typedef struct {
        RedType r[MAXSIZE+1];
        int length;
    }SqList;
    
    typedef SqList HeapType;    // 堆采用顺序表存储表示
    
    
    void main()
    {
        // 函数声明
        void CreateSqList(SqList &L);
        void ShowList(SqList L);
        void InsertSort(SqList &L);
        void QuickSort(SqList &L);
        void SelectSort(SqList &L);
        void HeapSort(HeapType &H);
        void BubbleSort(SqList &L);
    
        SqList L;
    
        printf("
    *********************** 排 序 ***********************
    
    ");
        
        CreateSqList(L);    // 创建顺序表
    
        int selectIndex = 0;
        while(true)
        {
            printf("
    
    (1)、插入排序
    ");
            printf("(2)、冒泡排序
    ");
            printf("(3)、快速排序
    ");
            printf("(4)、简单选择排序
    ");
            printf("(5)、堆排序
    ");
            printf("(6)、重新建立一个新的顺序表
    ");
            printf("(0)、退出程序
    
    ");
            printf("
    ----------------------请选择您希望的操作序号:");
            scanf("%d", &selectIndex);
            switch(selectIndex)
            {
            case 1:
                InsertSort(L);
                ShowList(L);
                break;
            case 2:
                BubbleSort(L);
                ShowList(L);
                break;
            case 3:
                QuickSort(L);
                ShowList(L);
                break;
            case 4:
                SelectSort(L);
                ShowList(L);
                break;
            case 5:
                HeapSort(L);
                ShowList(L);
                break;
            case 6:
                CreateSqList(L);
                break;
            case 0:
                return;
            default:
                break;
            }
            printf("
    ");
            system("pause");
        }
    
    
        system("pause");
    }
    
    
    
    bool EQ(KeyType a, KeyType b)    { return a == b; }
    bool LT(KeyType a, KeyType b)    { return a < b; }
    bool LQ(KeyType a, KeyType b)    { return a <= b; }
    
    void CreateSqList(SqList &L)
    {
        int length, i;
        printf("请构造一个任意的顺序表,以备接下来的排序所用: 
    ");
        printf("请输入顺序表的长度: 
    ");
        scanf("%d", &length);
        L.length = length;
        for(i = 1; i <= L.length; i++)
        {
            printf("请输入第 <%d> 个元素的值: ", i);
            scanf("%d", &L.r[i].key);
        }
        printf("
    
    顺序表创建完成!
    
    ");
    }
    
    void ShowList(SqList L)
    {
        int i;
        printf("排序后的顺序表为: 
    ");
        for(i = 1; i <= L.length; i++)
        {
            printf("%d  ", L.r[i].key);
        }
    }
    
    void InsertSort(SqList &L) {  // 算法10.1
      // 对顺序表L作直接插入排序。
      int i,j;
      for (i=2; i<=L.length; ++i)
        if (LT(L.r[i].key, L.r[i-1].key)) { 
          // "<"时,需将L.r[i]插入有序子表
          L.r[0] = L.r[i];                 // 复制为哨兵
          for (j=i-1;  LT(L.r[0].key, L.r[j].key);  --j)
            L.r[j+1] = L.r[j];             // 记录后移
          L.r[j+1] = L.r[0];               // 插入到正确位置
        }
    } // InsertSort
    
    int Partition(SqList &L, int low, int high) {  // 算法10.6(b) 快速排序
       // 交换顺序表L中子序列L.r[low..high]的记录,使枢轴记录到位,
       // 并返回其所在位置,此时,在它之前(后)的记录均不大(小)于它
       KeyType pivotkey;
       L.r[0] = L.r[low];            // 用子表的第一个记录作枢轴记录
       pivotkey = L.r[low].key;      // 枢轴记录关键字
       while (low<high) {            // 从表的两端交替地向中间扫描
          while (low<high && L.r[high].key>=pivotkey) --high;
          L.r[low] = L.r[high];      // 将比枢轴记录小的记录移到低端
          while (low<high && L.r[low].key<=pivotkey) ++low;
          L.r[high] = L.r[low];      // 将比枢轴记录大的记录移到高端
       }
       L.r[low] = L.r[0];            // 枢轴记录到位
       return low;                   // 返回枢轴位置
    } // Partition
    
    //int Partition(SqList &L, int low, int high) {  // 算法10.6(a)
    //   // 交换顺序表L中子序列L.r[low..high]的记录,使枢轴记录到位,
    //   // 并返回其所在位置,此时,在它之前(后)的记录均不大(小)于它
    //   KeyType pivotkey;
    //   RedType temp;
    //   pivotkey = L.r[low].key;     // 用子表的第一个记录作枢轴记录
    //   while (low<high) {           // 从表的两端交替地向中间扫描
    //      while (low<high && L.r[high].key>=pivotkey) --high;
    //      temp=L.r[low];
    //      L.r[low]=L.r[high];
    //      L.r[high]=temp;           // 将比枢轴记录小的记录交换到低端
    //      while (low<high && L.r[low].key<=pivotkey) ++low;
    //      temp=L.r[low];
    //      L.r[low]=L.r[high];
    //      L.r[high]=temp;           // 将比枢轴记录大的记录交换到高端
    //   }
    //   return low;                  // 返回枢轴所在位置
    //} // Partition
    
    void QSort(SqList &L, int low, int high) {  //算法10.7
      // 对顺序表L中的子序列L.r[low..high]进行快速排序
      int pivotloc;
      if (low < high) {                      // 长度大于1
        pivotloc = Partition(L, low, high);  // 将L.r[low..high]一分为二
        QSort(L, low, pivotloc-1); // 对低子表递归排序,pivotloc是枢轴位置
        QSort(L, pivotloc+1, high);          // 对高子表递归排序
      }
    } // Qsort
    
    void QuickSort(SqList &L) {  // 算法10.8
       // 对顺序表L进行快速排序
       QSort(L, 1, L.length);
    } // QuickSort
    
    // 在L.r[i..L.length]中选择key最小的记录
    int SelectMinKey(SqList L, int i)
    {
        int k = i;
        int min = L.r[i].key;
        for( ; i <= L.length; i++ )
        {
            if(L.r[i].key < min)
            {
                min = L.r[i].key;
                k = i;
            }
        }
        return k;
    }
    
    void SelectSort(SqList &L) {  // 算法10.9
      // 对顺序表L作简单选择排序。
      int i,j;
      for (i=1; i<L.length; ++i) { // 选择第i小的记录,并交换到位
        j = SelectMinKey(L, i);  // 在L.r[i..L.length]中选择key最小的记录
        if (i!=j) {                // L.r[i]←→L.r[j];   与第i个记录交换
          RedType temp;
          temp=L.r[i];
          L.r[i]=L.r[j];
          L.r[j]=temp;    
        } 
      }
    } // SelectSort
    
    void HeapAdjust(HeapType &H, int s, int m) {  // 算法10.10
      // 已知H.r[s..m]中记录的关键字除H.r[s].key之外均满足堆的定义,
      // 本函数调整H.r[s]的关键字,使H.r[s..m]成为一个大顶堆
      // (对其中记录的关键字而言)
      int j;
      RedType rc;
      rc = H.r[s];
      for (j=2*s; j<=m; j*=2) {   // 沿key较大的孩子结点向下筛选
        if (j<m && H.r[j].key<H.r[j+1].key) ++j; // j为key较大的记录的下标
        if (rc.key >= H.r[j].key) break;         // rc应插入在位置s上
        H.r[s] = H.r[j];  s = j;
      }
      H.r[s] = rc;  // 插入
    } // HeapAdjust
    
    void HeapSort(HeapType &H) {  // 算法10.11
       // 对顺序表H进行堆排序。
       int i;
       RedType temp;
       for (i=H.length/2; i>0; --i)  // 把H.r[1..H.length]建成大顶堆
          HeapAdjust ( H, i, H.length );
          for (i=H.length; i>1; --i) {
             temp=H.r[i];
             H.r[i]=H.r[1];
             H.r[1]=temp;  // 将堆顶记录和当前未经排序子序列Hr[1..i]中
                           // 最后一个记录相互交换
             HeapAdjust(H, 1, i-1);  // 将H.r[1..i-1] 重新调整为大顶堆
          }
    } // HeapSort
    
    // 冒泡排序
    void BubbleSort(SqList &L)
    {
        int i, j, temp;
        bool change = TRUE;
        for( i = L.length; i >= 1 && change; --i )
        {
            change = FALSE;
            for( j =0; j < i; ++j )
            {
                if( L.r[j].key > L.r[j+1].key )
                {
                    temp = L.r[j].key;
                    L.r[j].key = L.r[j+1].key;
                    L.r[j+1].key = temp;
                    change = TRUE;
                }
            }
        }
    }// Bubble_Sort

    可以直接复制上述代码,也可以去我的网盘下载源cpp文件

    http://pan.baidu.com/s/105Jqr

  • 相关阅读:
    array_map()与array_shift()搭配使用 PK array_column()函数
    Educational Codeforces Round 8 D. Magic Numbers
    hdu 1171 Big Event in HDU
    hdu 2844 poj 1742 Coins
    hdu 3591 The trouble of Xiaoqian
    hdu 2079 选课时间
    hdu 2191 珍惜现在,感恩生活 多重背包入门题
    hdu 5429 Geometric Progression 高精度浮点数(java版本)
    【BZOJ】1002: [FJOI2007]轮状病毒 递推+高精度
    hdu::1002 A + B Problem II
  • 原文地址:https://www.cnblogs.com/uppercloud/p/DataStructure_Sort.html
Copyright © 2011-2022 走看看