排序算法C++实现

Posted on 2018-08-10 10:16  Apelike  阅读(180)  评论(0编辑  收藏  举报

先按照王道系列的伪代码,写了一下常见的排序算法。代码先放这儿,先不做算法分析,回头再来分析消化。

 

 

// 排序算法.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<vector>
using namespace std;
//插入排序——直接插入排序
void InsertSort(vector<int>&vctArray)
{
    if (vctArray.size() <= 1)
        return;
    int i, j, tmp;
    for ( i = 1; i < vctArray.size(); ++i)
    {
        if (vctArray[i] < vctArray[i - 1])
        {
            tmp = vctArray[i];
            for ( j = i-1; j >= 0 && tmp<vctArray[j]; --j)
                vctArray[j + 1] = vctArray[j]; 
            vctArray[j+1] = tmp;        
        }
    }
}
//插入排序——折半插入排序(算法复杂度:仅仅改变比较次数,移动次数仍然不变)
void HalfInsertSort(vector<int>&vctArray)
{
    if (vctArray.size() <= 1)
        return;
    int i, j, tmp;
    for (i = 1; i < vctArray.size(); ++i)
    {
        if (vctArray[i] < vctArray[i - 1])
        {
            int begin = 0, end = i - 1, mid;
            tmp = vctArray[i];
            while (begin<=end)
            {
                mid = (begin + end) / 2;
                if (vctArray[mid] > tmp)
                    end = mid -= 1;
                else
                    begin = mid + 1;
            }
            for (j = i - 1; j >end; --j)
                vctArray[j + 1] = vctArray[j];
            vctArray[end+1] = tmp;
        }
    }
}
//插入排序——希尔排序
void ShellSort(vector<int>&vctArray)
{
    int dk,i,j,tmp;
    for (dk = vctArray.size() / 2; dk >= 1; dk = dk / 2)
    {
        for (i = dk + 1; i < vctArray.size(); ++i)
        {
            if (vctArray[i] < vctArray[i - dk])
            {
                tmp = vctArray[i];
                for (j = i - dk; j >= 0 && vctArray[j] > tmp; j -= dk)
                    vctArray[j + dk] = vctArray[j];
                vctArray[j + dk] = tmp;
            }
        }
    }
}
//交换排序——冒泡排序
void BubbleSort(vector<int>& vctArray)
{
    int i, j, tmp;
    bool flag = true;
    for (i = 0; i < vctArray.size()&&flag; ++i)
    {
        flag = false;
        for (j = vctArray.size() - 1; j > i; --j)
        {
            if (vctArray[j] < vctArray[j - 1])
            {
                tmp = vctArray[j - 1];
                vctArray[j - 1] = vctArray[j];
                vctArray[j] = tmp;
                flag = true;
            }
        }
    }
}
//交换排序——快速排序
int Partition(vector<int>&vctArray, int low, int high);
void QuickSort(vector<int>& vctArray,int low,int high)
{
    if (low < high)
    {
        int mid=Partition(vctArray,low,high);
        QuickSort(vctArray,low, mid - 1);
        QuickSort(vctArray, mid + 1,high);
    }
}
int Partition(vector<int>&vctArray, int low, int high)
{
    int pivot=vctArray[low];
    while (low < high)
    {
        while (low < high&&vctArray[high] >= pivot)
            --high;
        vctArray[low] = vctArray[high];
        while (low < high&&vctArray[low] <= pivot)
            ++low;
        vctArray[high] = vctArray[low];
    }
    vctArray[low] = pivot;    
    return low;
}

//选择排序——简单排序
void SelectSort(vector<int>& vctArray)
{
    int minIndex = -1;
    for (int i = 0; i < vctArray.size()-1; ++i)
    {
        minIndex = i;
        for (int j = i + 1; j < vctArray.size(); ++j)
        {
            if (vctArray[j] < vctArray[minIndex])
                minIndex = j;
        }
        if (minIndex != i)
        {
            int tmp = vctArray[minIndex];
            vctArray[minIndex] = vctArray[i];
            vctArray[i] = tmp;
        }
    }
}
//选择排序——堆排序
void BuildMaxHeap(vector<int>&vctArray, int len);
void AdjustDown(vector<int>&vctArray,int k,int len);
void MaxHeapSort(vector<int>& vctArray)
{
    BuildMaxHeap(vctArray,vctArray.size()-1);
    for (int i = vctArray.size()-1; i >=1; --i)
    {
        vctArray[0] = vctArray[1];
        vctArray[1] = vctArray[i];
        vctArray[i] = vctArray[0];
        AdjustDown(vctArray,1,i-1);
    }
}
void BuildMaxHeap(vector<int>&vctArray,int len)
{
    for (int i = len / 2; i > 0; --i)
        AdjustDown(vctArray,i,len);
}
void AdjustDown(vector<int>&vctArray,int k,int len)
{
    vctArray[0] = vctArray[k];
    for (int i = 2 * k; i <= len; i*=2)
    {
        if (i < len&& vctArray[i] < vctArray[i + 1])
            ++i;
        if (vctArray[0] >= vctArray[i])
            break;
        else
        {
            vctArray[k] = vctArray[i];
            k = i;
        }
    }
    vctArray[k]= vctArray[0];
}
//归并排序
void Merge(vector<int>&vctArray, int low, int mid, int high);
void MergeSort(vector<int>&vctArray, int low, int high)
{
    if (low < high)
    {
        int mid = (low + high) / 2;
        MergeSort(vctArray,low,mid);
        MergeSort(vctArray,mid+1,high);
        Merge(vctArray,low,mid,high);
    }
}
void Merge(vector<int>&vctArray, int low, int mid, int high) 
{
    vector<int> vctTmp = vctArray;
    int i, j, k;
    for (i = low, j = mid + 1, k = i; i <= mid&&j <= high; ++k)
    {
        if (vctTmp[i] <= vctTmp[j])
            vctArray[k] = vctTmp[i++];
        else
            vctArray[k] = vctTmp[j++];
    }
    while (i<=mid)
        vctArray[k++] = vctTmp[i++];
    while (j <= high)
        vctArray[k++] = vctTmp[j++];
}

void PrintArray(vector<int>& vctArray,int k)
{
    for (int i = k; i < vctArray.size(); ++i)
    {
        cout << vctArray[i] << " ";
    }
    cout << endl;
}
int main()
{
    vector<int> vctArray = { 11,10,9,8,7,6,5,4,2,1,0 };
//    vector<int> vctArray = { 3,6,1,8,3,6,2,8,4,5,9 };
    vector<int> tmpArray = vctArray;
    cout << endl;
    cout << "..........插入排序——直接插入排序..........." << endl;
    cout << "排序前:";
    PrintArray(vctArray,0);
    InsertSort(vctArray);
    cout << "排序后:";
    PrintArray(vctArray, 0);
    
    cout << endl;
    vctArray = tmpArray;
    cout << endl;

    cout << "..........插入排序——折半插入排序..........." << endl;
    cout << "排序前:";
    PrintArray(vctArray, 0);
    InsertSort(vctArray);
    cout << "排序后:";
    PrintArray(vctArray, 0);

    cout << endl;
    vctArray = tmpArray;
    cout << endl;

    cout << "............插入排序——希尔排序............." << endl;
    cout << "排序前:";
    PrintArray(vctArray, 0);
    ShellSort(vctArray);
    cout << "排序后:";
    PrintArray(vctArray, 0);

    cout << endl;
    vctArray = tmpArray;
    cout << endl;

    cout << "............交换排序——冒泡排序............." << endl;
    cout << "排序前:";
    PrintArray(vctArray, 0);
    BubbleSort(vctArray);
    cout << "排序后:";
    PrintArray(vctArray, 0);

    cout << endl;
    vctArray = tmpArray;
    cout << endl;

    cout << "............交换排序——快速排序............." << endl;
    cout << "排序前:";
    PrintArray(vctArray, 0);
    QuickSort(vctArray,0,vctArray.size()-1);
    cout << "排序后:";
    PrintArray(vctArray, 0);

    cout << endl;
    vctArray = tmpArray;
    cout << endl;

    cout << "..........选择排序——简单选择排序..........." << endl;
    cout << "排序前:";
    PrintArray(vctArray, 0);
    SelectSort(vctArray);
    cout << "排序后:";
    PrintArray(vctArray, 0);

    cout << endl;
    vctArray = tmpArray;
    cout << endl;

    cout << ".............选择排序——最大堆排序..........." << endl;
    cout << "排序前:";
    PrintArray(vctArray, 0);
    //为了方便操作,从1开始存放元素,即下标1为跟节点,将第0个元素设为哨兵
    vctArray.push_back(vctArray[0]);
    MaxHeapSort(vctArray);
    cout << "排序后:";
    PrintArray(vctArray, 1);

    cout << endl;
    vctArray = tmpArray;
    cout << endl;

    cout << "...................归并排序...................." << endl;
    cout << "排序前:";
    PrintArray(vctArray, 0);
    MergeSort(vctArray,0,vctArray.size()-1);
    cout << "排序后:";
    PrintArray(vctArray, 0);
    cout << endl;

    return 0;
}