各种排序算法集合

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
void InsertSort(int *a, int n);
void ShellInsertSort(int *a, int n, int d);
void ShellSort(int *a, int n);
void SelectSort(int *a, int n);
void AdjustHeap(int *a, int start, int end);
void BuildHeap(int *a, int n);
void HeapSort(int *a, int n);
int  Partition(int *a, int start, int end);
void QuickSort(int *a, int start, int end);

int main(int argc, char *argv[])
{
    int n, *a;
    cout << "please input the size of the array" << endl;
    cin >> n;
    a = (int*)malloc((n + 1)*sizeof(int));
    cout << "input the " << n << " number " << endl;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    //InsertSort(a, n);
    //ShellSort(a, n);
    //void HeapSort(int *a, int n);
    //SelectSort(a, n);
    QuickSort(a, 0, n - 1);
    cout << "after sort " << endl;
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    system("pause");
}
void InsertSort(int *a, int n)//插入排序-简单插入排序
{
    int j;
    int x;
    for (int i = 1; i < n; i++)
    {
        if (a[i] < a[i - 1])
        {
            j = i - 1;
            x = a[i];
            while (j >= 0 && a[j] > x)
            {
                a[j + 1] = a[j];
                j--;
            }
            a[j + 1] = x;
        }
    }
}
void ShellSort(int *a, int n)//插入排序-希尔排序
{
    int d = n / 2;
    while (d != 0)
    {
        ShellInsertSort(a, n, d);
        d /= 2;
    }
}
void ShellInsertSort(int *a, int n, int d)//以d为增量来进行希尔排序
{
    int x, j;
    for (int i = d; i < n; i++)
    {
        if (a[i] < a[i - d])
        {
            x = a[i];
            j = i - d;
            while (j >= 0 && a[j]>x)
            {
                a[j + d] = a[j];
                j -= d;
            }
            a[j + d] = x;
        }
    }
}
void SelectSort(int *a, int n)//选择排序-简单选择排序
{
    int minindex, temp;
    for (int i = 0; i < n - 1; i++)
    {
        minindex = i;
        for (int j = i + 1; j < n; j++)
        {
            if (a[j] < a[minindex])
                minindex = j;
        }
        if (minindex != i)
        {
            temp = a[minindex];
            a[minindex] = a[i];
            a[i] = temp;
        }
    }
}
void HeapSort(int *a, int n)//选择排序-堆排序
{
    int temp;
    BuildHeap(a, n);//建堆
    for (int i = n - 1; i > 0; i--)
    {
        temp = a[0];
        a[0] = a[i];
        a[i] = temp;//将堆顶的值和下标为i的值进行交换
        AdjustHeap(a, 0, i);
    }
}
void BuildHeap(int *a, int n)//建堆
{
    for (int i = (n-2)/2; i >= 0; i++)//从最后一个有子节点的节点开始到根节点开始进行调整
    {
        AdjustHeap(a, i, n);
    }
}
void AdjustHeap(int *a, int start, int end)//start为堆顶 end为最后一个元素的下标加1 将其调整为一个大根堆
{
    int child, temp,f;
    f = start;//将开始节点作为最开始的父亲f节点
    temp = a[f];
    child = 2 * f + 1;//得到f节点的左孩子节点的下标
    while (child < end)
    {
        if (child + 1 < end&&a[child] < a[child + 1])//判断有孩子节点是否在下标范围内 且使得child下标指向最大的孩子
            child++;
        if (a[child] > temp)
        {
            a[f] = a[child];
            f = child;
            child = 2 * f + 1;
        }
        else
            break;
    }
    a[f] = temp;
}
void QuickSort(int *a, int start, int end)//交换排序-快速排序
{

    if (end > start)
    {
        int partition = Partition(a, start, end);
        QuickSort(a, start, partition - 1);
        QuickSort(a, partition + 1, end);
    }
}
int Partition(int *a, int start, int end)//确定基准点的位置 并返回基准点最后的位置下标
{
    int temp = a[start];
    while (start < end)
    {
        while (start < end&&a[end] >= temp) end--;
        if (start < end) a[start] = a[end];
        while (start < end&&a[start] <= temp)start++;
        if (start < end)a[end] = a[start];
    }
    a[start] = temp;
    return start;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hebastast

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值