// 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;
}
各种排序算法集合
最新推荐文章于 2022-10-24 21:06:57 发布