直接上demo:
#include <iostream>
using namespace std;
// 冒泡排序:
void BubbleSort(int a[], int n)
{
bool flag = true;
int k = n;
while (flag)
{
flag = false;
for (int i = 1; i < k; i++)
{
if (a[i - 1] > a[i]) {
swap(a[i - 1], a[i]);
flag = true;
}
}
k--;
}
}
// 直接插入排序:
void Insertsort(int a[], int n)
{
int i, j;
for (i = 1; i < n; i++)
{
for (j = i - 1; j >= 0; j--)
{
if (a[j + 1] < a[j])
swap(a[j], a[j + 1]);
}
}
}
// 希爾排序(分組插入排序宪巨,增量為d,d=1是直接插入排序):
void Shellsort(int a[], int n) {
int i, j, gap;
for (gap = n / 2; gap > 0; gap /= 2)
{
//for (i = 0; i < gap; i++)
for (i = gap; i < n; i++)
{
for (j = i - gap; j >= 0; j -= gap)
{
if (a[j + gap] < a[j])
swap(a[j + gap], a[j]);
}
}
}
}
// 直接選擇排序:
void Selectsort(int a[], int n) {
int i, j;
for (i = 0; i < n; i++)
{
int minIndex = i;
for (j = i + 1; j < n; j++)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}
swap(a[i], a[minIndex]);
}
}
// 歸并排序(分治法):
// 將有序數(shù)組a[first...mid]和a[mid...last]合并
void MergeArray(int a[], int first, int mid, int last, int tmp[])
{
int i = first, n = mid;
int j = mid + 1, m = last;
int k = 0;
while (i <= n&&j <= m)
{
if (a[i] < a[j])
tmp[k++] = a[i++];
else
tmp[k++] = a[j++];
}
while (i <= n)
{
tmp[k++] = a[i++];
}
while (j <= m)
tmp[k++] = a[j++];
for (i = 0; i < k; i++)
a[first + i] = tmp[i];
}
// 先遞歸地分解數(shù)列垃它,再合并數(shù)列完成歸并排序:
void MergesortSection(int a[], int first, int last, int tmp[])
{
if (first < last)
{
int mid = (first + last) / 2;
MergesortSection(a, first, mid, tmp); // 左邊有序
MergesortSection(a, mid + 1, last, tmp); // 右邊有序
MergeArray(a, first, mid, last, tmp); // 合并兩個(gè)有序序列
}
}
void Mergesort(int a[], int n)
{
int *p = new int[n];
if (p == NULL)
return;
MergesortSection(a, 0, n - 1, p);
delete[] p;
}
// 快速排序:
// 找中樞(挖坑填數(shù))
int Partition(int a[], int left, int right)
{
int flag = a[left];
while (left < right)
{
while (a[right] >= flag&&left < right)
right--;
if (left < right)
{
a[left] = a[right];
left++;
}
while (a[left] < flag&&left < right)
left++;
if (left < right)
{
a[right] = a[left];
right--;
}
}
a[left] = flag;
return left;
}
void QuicksortSection(int a[], int left, int right)
{
if (left < right)
{
int i = Partition(a, left, right);
QuicksortSection(a, left, i -1);
QuicksortSection(a, i + 1, right);
}
}
void Quicksort(int a[], int n)
{
int left = 0;
int right = n - 1;
QuicksortSection(a, left, right);
}
// 堆排序:
// 新加入節(jié)點(diǎn)i 其父節(jié)點(diǎn)為(i-1)/2
void MinHeapFixUp(int a[], int i)
{
for (int j = (i - 1) / 2; a[i] > a[j] && i != 0 && j >= 0; i = j, j = (i - 1) / 2)
swap(a[i], a[j]);
}
// 插入節(jié)點(diǎn)勺良,只能在最小堆的末尾插入
void MinHeapAddNumber(int a[], int n, int nNum)
{
a[n] = nNum;
MinHeapFixUp(a, n);
}
// 刪除節(jié)點(diǎn)i 調(diào)整堆 只能刪除最小堆的堆頂元素唯卖,將最后一個(gè)元素a[n]放在堆頂眠寿,進(jìn)行調(diào)整
void MinHeapFixDown(int a[], int i, int n)
{
int j, tmp;
tmp = a[i];
j = 2 * i + 1; // 左兒子
while (j < n)
{
if (j + 1 < n&&a[j + 1] < a[j]) // 在左兒子和右兒子中找最小
j++;
if (a[j] >= tmp)
break;
swap(a[j], a[i]);
i = j;
j = 2 * i + 1;
}
a[i] = tmp;
}
void MinHeapDeleteNumber(int a[], int n)
{
swap(a[0], a[n - 1]);
MinHeapFixDown(a, 0, n - 1);
}
// 建立最小堆,數(shù)組畫(huà)成堆后,葉子節(jié)點(diǎn)已經(jīng)是合法的堆了查乒,只要從最后一個(gè)葉子(即數(shù)組的最后一個(gè)元素)的雙親節(jié)點(diǎn)弥喉,向下調(diào)整就好
void MakeMinHeap(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
MinHeapFixDown(a, i, n);
}
// 堆排序,對(duì)于小頂堆玛迄,我們只能從刪除堆頂元素開(kāi)始
// 對(duì)于堆用數(shù)組表示由境,我們將堆頂元素a[0]與a[n-1]交換, a[1]與a[n-2]交換...
// 相當(dāng)于刪除堆頂元素蓖议,所以向下調(diào)整堆
// 結(jié)果小頂堆對(duì)應(yīng)的是從大到小的排序虏杰,若要從小到大,需要對(duì)結(jié)果逆序(或者用大頂堆)
void Minheapsort(int a[], int n)
{
MakeMinHeap(a, 5);
for (int i = n - 1; i >= 1; i--)
{
swap(a[i], a[0]);
MinHeapFixDown(a, 0, i);
}
}
int main()
{
int a[] = { 5,3,4,1,2 };
BubbleSort(a, 5);
Insertsort(a, 5);
Shellsort(a, 5);
Selectsort(a, 5);
Mergesort(a, 5);
Quicksort(a, 5);
Minheapsort(a, 5);
for (int i = 0; i < 5; i++)
{
cout << a[i] << ",";
}
system("pause");
return 0;
}