zoukankan      html  css  js  c++  java
  • 简单选择排序和堆排序

    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    #define MAXSIZE 20
    typedef struct
    {
        int r[MAXSIZE + 1];
        int length;
    }SqList;
    
    typedef SqList HeapType;
    
    //***********************************选择排序*************************************begin
    //查找最小值
    int SelectMinKey(const SqList& L, int start)
    {
        int minValue = L.r[start];
        int index = start;
        for (int i = start; i <= L.length; i++)
        {
            if (minValue > L.r[i])
            {
                minValue = L.r[i];
                index = i;
            }
        }
        return index;
    }
    
    //简单选择排序
    void SelectSort(SqList &L)
    {
        for (int i = 1; i <= L.length; i++)
        {
            int j = SelectMinKey(L, i);
            if (i != j)
            {
                int tmp = L.r[i];
                L.r[i] = L.r[j];
                L.r[j] = tmp;
            }
        }
    }
    
    void SqlistPrint(SqList &L)
    {
        for (int i = 1; i <= L.length; i++)
        {
            cout<<L.r[i]<<"  ";
        }
        cout<<endl;
    }
    
    //***********************************选择排序*************************************end
    
    //***********************************堆排序*************************************begin
    //堆调整
    void HeadAdjust(HeapType &H,int s,int m)
    {
        int rc = H.r[s];
        for (int j = 2 * s; j <= m; j *= 2)
        {
            if (j < m && H.r[j] < H.r[j + 1])
            {
                ++j;
            }
            if (rc > H.r[j])
            {
                break;
            }
            H.r[s] = H.r[j];
            s = j;
        }
        H.r[s] = rc;
    }
    
    //堆排序
    void HeadSort(HeapType &H)
    {
        for (int i = H.length / 2; i > 0; --i)
        {
            HeadAdjust(H, i, H.length);
        }
        for (int i = H.length; i > 1; --i)
        {
            int tmp = H.r[1];
            H.r[1] = H.r[i];
            H.r[i] = tmp;
            HeadAdjust(H, 1, i - 1);
        }
    }
    
    void HeadPrint(HeapType &H)
    {
        for (int i = 1; i <= H.length; i++)
        {
            cout<<H.r[i]<<"  ";
        }
        cout<<endl;
    }
    
    //***********************************堆排序*************************************end
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int arr[8] = {49, 38, 65, 97, 76, 13, 27, 49};
        SqList L;
        HeapType H;
        L.length = H.length = sizeof(arr)/sizeof(arr[0]);
        for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
        {
            L.r[i + 1] = arr[i];
            H.r[i + 1] = arr[i];
        }
        cout<<"*************************简单排序**************************"<<endl;
        cout<<"before: ";
        SqlistPrint(L);
        SelectSort(L);
        cout<<"after:  ";
        SqlistPrint(L);
    
        cout<<endl<<"**************************堆排序***************************"<<endl;
        cout<<"before: ";
        HeadPrint(H);
        HeadSort(H);
        cout<<"after:  ";
        HeadPrint(H);
    
        return 0;
    }

    运行界面如下:

  • 相关阅读:
    Shapefile文件数据库操作ArcEngine +C#
    INewFeedBack接口ArcGlobe
    如何获取免费Aster GDem数据
    FUCK EFS!!!!!
    5种方法解除开机密码[转]
    blobtracking references[转]
    cvMatND
    二值图像相似性[转]
    OpenCV中打印CvMat的元素
    视觉&图像处理相关链接
  • 原文地址:https://www.cnblogs.com/venow/p/2660343.html
Copyright © 2011-2022 走看看