冒泡排序、插入排序、计数排序、归并排序、快速排序、堆排序

//subFun.h

#pragma once
#include <vector>
#include <algorithm>

void Merge_Sort(int *input, int p, int r);
void Merge(int *input, int p, int q, int r);
int Partition(int *input, int p, int r);
void myQuickSort(int *input, int p, int r);
void Heapfy(int *input, int i, const int HEAP_SIZE);
void BuildHeap(int *input, const int HEAP_SIZE);
void myHeapSort(int *input, int n);
void mySort(int *input, int HEAP_SIZE);


//subFun.cpp

#include "subFun.h"

void Merge_Sort(int *input, int p, int r)
{
    if(p < r)
    {
        int q = (p + r) / 2;
        Merge_Sort(input, p, q);
        Merge_Sort(input, q + 1, r);
        Merge(input, p, q, r);
    }
}

//自己写的,正确
//void Merge(int *input, int p, int q, int r)
//{
//    std::vector<int> tmp(input + p, input + p + r - p + 1);
//    std::sort(tmp.begin(), tmp.end());
//    memcpy(input + p, (int*)&tmp[0], (r - p + 1) * sizeof(int));
//}

//算法书上的,正确
void Merge(int *input, int p, int q, int r)
{
    int ln = q - p + 1;
    int rn = r - q;
    std::vector<int> left(input + p, input + p + ln);
    std::vector<int> right(input + p + ln, input + p + ln + rn);
    left.push_back(65535);
    right.push_back(65535);
    int i = 0, j = 0;
    for(int k = p; k <= r; ++k)
    {
        if(left[i] <= right[j])
        {
            input[k] = left[i];
            ++i;
        }
        else
        {
            input[k] = right[j];
            ++j;
        }
    }
}

void myQuickSort(int *input, int p, int r)
{
    if(p < r)
    {
        int q = Partition(input, p, r);
        myQuickSort(input, p, q - 1);
        myQuickSort(input, q + 1, r);
    }
}

int Partition(int *input, int p, int r)
{
    int key = input[r];
    int i = p - 1;
    int j = p;
    for(; j < r; ++j)
    {
        if(key > input[j])
        {
            std::swap(input[i + 1], input[j]);
            ++i;
        }
    }
    std::swap(input[i + 1], input[r]);
    return i + 1;
}

//小顶堆排序
void myHeapSort(int *input, int n)
{
    int HEAP_SIZE = n;
    BuildHeap(input, HEAP_SIZE);
    mySort(input, HEAP_SIZE);
}

void mySort(int *input, int HEAP_SIZE)
{
    for(; HEAP_SIZE > 0; )
    {
        std::swap(input[0], input[--HEAP_SIZE]);
        Heapfy(input, 1, HEAP_SIZE);
    }
}

void BuildHeap(int *input, int HEAP_SIZE)
{
    for(int j = HEAP_SIZE / 2; j > 0; --j)
    {
        Heapfy(input, j, HEAP_SIZE);
    }
}

//维护堆的性质
void Heapfy(int *input, int i, int HEAP_SIZE)
{
    int left = 2 * i;
    int right = 2 * i + 1;
    int max = i;
    if(left <= HEAP_SIZE && input[left - 1] > input[max - 1])
    {
        max = left;
    }
    if(right <= HEAP_SIZE && input[right - 1] > input[max - 1])
    {
        max = right;
    }
    if(max != i)
    {
        std::swap(input[i - 1], input[max - 1]);
        Heapfy(input, max, HEAP_SIZE);
    }
}



//main.cpp

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <windows.h>
#include <numeric>
#include <functional>
#include "subFun.h"

void mySort(int *input, int n, int *output)
{
    std::vector<int> vecIn(input, input + n);
    std::sort(vecIn.begin(), vecIn.end(), std::greater<int>());
    int m = n / 2;
    output[m] = vecIn.front();
    int i = m - 1;
    int j = m + 1;
    for(int k = 1; k < n; ++k)
    {
        if(i >= 0)
        {
            output[i] = vecIn[k];
            --i;
        }
        if(j < n)
        {
            output[j] = vecIn[++k];
            ++j;
        }
    }
}

//冒泡排序
void BubbleSort(int *input, int n, int *output)
{
    for(int i = 0; i < n; ++i)
    {
        for(int j = i; j < n; ++j)
        {
            if(input[i] > input[j])
            {
                std::swap(input[i], input[j]);
            }
        }
    }
    memcpy(output, input, n * sizeof(int));
}

//归并排序
void MergeSort(int *input, int n, int *output)
{
    Merge_Sort(input, 0, n - 1);
    memcpy(output, input, n * sizeof(int));
}

//快速排序
void QuickSort(int *input, int n, int *output)
{
    myQuickSort(input, 0, n - 1);
    memcpy(output, input, n * sizeof(int));
}

//堆排序
void HeapSort(int *input, int n, int *output)
{
    myHeapSort(input, n);
    memcpy(output, input, n * sizeof(int));
}

//插入排序
void InsertionSort(int *input, int n, int *output)
{
    //for(int j = 1; j < n; ++j)
    //{
    //    for(int i = j - 1; i >= 0; --i)
    //    {
    //        if(input[j] >= input[i])
    //        {
    //            break;
    //        }
    //        if(input[j] < input[i])
    //        {
    //            std::swap(input[j], input[i]);
    //            --j;
    //        }
    //    }
    //}
    for(int j = 1; j < n; ++j)
    {
        int key = input[j];
        for(int i = j - 1; i >= 0 && input[i] > key; --i)
        {
            std::swap(input[i], input[i + 1]);
        }
    }
    memcpy(output, input, n * sizeof(int));
}

//计数排序
void CountSort(int *input, int n, int *output)
{
    int max = 0;
    for(int i = 0; i < n; ++i)
    {
        if(max < input[i])
        {
            max = input[i];
        }
    }

    std::vector<int> c(max + 1, 0);
    for(int i = 0; i < n; ++i)
    {
        ++c[input[i]];
    }
    for(int i = 1; i <= max; ++i)
    {
        c[i] += c[i - 1];
    }

    for(int i = n - 1; i >= 0; --i)
    {
        int tmp = c[input[i]];
        output[tmp - 1] = input[i];//input[i]应放在tmp-1这个位置,因为<=input[i]的有tmp个
        --c[input[i]];
    }
}

int main()
{
    int n;
    std::cout << "请输入数组大小:";
    std::cin >> n;
    int *input = new int[n];
    int *output = new int[n];
    for(int i = 0; i < n; ++i)
    {
        //std::cout << "请输入元素:";
        std::cin >> input[i];
    }
    //mySort(input, n, output);
    //BubbleSort(input, n, output);//冒泡排序
    //InsertionSort(input, n, output);//插入排序
    //CountSort(input, n, output);//计数排序
    //MergeSort(input, n, output);//归并排序
    //QuickSort(input, n, output);//快速排序
    HeapSort(input, n, output);//堆排序
    delete[] input;
    for(int i = 0; i < n; ++i)
    {
        std::cout << output[i] << " ";
    }
    delete[] output;
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值