1、插入排序
2、希尔排序
3、冒泡排序
4、快速排序
5、简单选择排序
所有排序为了表述方便,所用存储结构的0号位置都留空作为哨兵。
/***************************************************************
1、插入排序
2、希尔排序
3、冒泡排序
4、快速排序
5、简单选择排序
所有排序为了表述方便,所用存储结构的0号位置留空作为哨兵
****************************************************************/
#include<iostream>
using namespace std;
//插入排序
void InsertSort(int r[], int n) {
for (int i = 2; i <= n; i++) {
if (r[i] < r[i - 1]) {
r[0] = r[i];//将要插入的值赋值给哨兵
int j = i - 1;
for (j; r[0] < r[j]; j--)//从后向前进行顺序查找
r[j + 1] = r[j];
r[j + 1] = r[0];
}
}
}
//希尔排序
void ShellInsert(int r[], int n) {
for (int d = n / 2; d >= 1; d = d / 2) {//以d为增量
for (int i = d + 1; i <= n; i++) {//一趟希尔排序
if (r[i] < r[i - d]) {
r[0] = r[i];
int j = i - d;
for (; j > 0 && r[0] < r[j]; j = j - d)
r[j + d] = r[j];
r[j + d] = r[0];
}
}
}
}
//冒泡排序
void BubbleSort(int r[], int n) {
int pos = n;
while (pos != 0) {
int bound = pos;
pos = 0;
for (int i = 1; i < bound; i++) {
if (r[i] > r[i + 1]) {
r[0] = r[i];
r[i] = r[i + 1];
r[i + 1] = r[0];
pos = i;//记录位置
}
}
}
}
//一趟快速排序
int Partion(int r[], int first,int end){
int i = first;
int j = end;
int pivot = r[i];//选取基准元素
while (i < j) {
while ((i < j) && (r[j] >= pivot)) //右侧扫描
j--;
r[i] = r[j];
while ((i < j) && (r[i] <= pivot))
i++;
r[j] = r[i];
}
r[i] = pivot;
return i;//返回轴值位置
}
//快速排序
void QuickSort(int r[], int i, int j) {
if (i < j) {//递归直到所有分区只有一个元素
int pivotLocation = Partion(r, i, j);
QuickSort(r, i, pivotLocation-1);
QuickSort(r, pivotLocation + 1, j);
}
}
//选择排序
void SelectSort(int r[], int n) {
for (int i = 1; i < n; i++) {
int index = i;
for (int j = i + 1; j <= n; j++)//查找最小记录index位置
if (r[j] < r[index])
index = j;
if (index != i) {
r[0] = r[i];
r[i] = r[index];
r[index] = r[0];
}
}
}
//测试函数
void TestA() {
cout << "Positive sequence data:" << endl;
int test1[6] = { NULL,1,2,3,4,5 };
cout << "The test array is:";
for (int i = 1; i <= 5; i++)
cout << test1[i] << ' ';
cout << endl;
InsertSort(test1, 5);
cout << "InnsertSort:";
for (int i = 1; i <= 5; i++)
cout << test1[i] << " ";
cout << endl;
int test2[6] = { NULL,1,2,3,4,5 };
ShellInsert(test2, 5);
cout << "ShellInsert:";
for (int i = 1; i <= 5; i++)
cout << test2[i] << " ";
cout << endl;
int test3[6] = { NULL,1,2,3,4,5 };
BubbleSort(test3, 5);
cout << "BubbleSort:";
for (int i = 1; i <= 5; i++)
cout << test3[i] << " ";
cout << endl;
int test4[6] = { NULL,1,2,3,4,5 };
QuickSort(test4, 1, 5);
cout << "QuickSort:";
for (int i = 1; i <= 5; i++)
cout << test4[i] << " ";
cout << endl;
int test5[6] = { NULL,1,2,3,4,5 };
SelectSort(test5, 5);
cout << "SelectSort:";
for (int i = 1; i <= 5; i++)
cout << test5[i] << " ";
cout << endl << endl;
}
void TestB() {
cout << "Reverse order data:" << endl;
int test1[6] = { NULL,5,4,3,2,1 };
cout << "The test array is:";
for (int i = 1; i <= 5; i++)
cout << test1[i] << ' ';
cout << endl;
InsertSort(test1, 5);
cout << "InnsertSort:";
for (int i = 1; i <= 5; i++)
cout << test1[i] << " ";
cout << endl;
int test2[6] = { NULL,5,4,3,2,1 };
ShellInsert(test2, 5);
cout << "ShellInsert:";
for (int i = 1; i <= 5; i++)
cout << test2[i] << " ";
cout << endl;
int test3[6] = { NULL,5,4,3,2,1 };
BubbleSort(test3, 5);
cout << "BubbleSort:";
for (int i = 1; i <= 5; i++)
cout << test3[i] << " ";
cout << endl;
int test4[6] = { NULL,5,4,3,2,1 };
QuickSort(test4, 1, 5);
cout << "QuickSort:";
for (int i = 1; i <= 5; i++)
cout << test4[i] << " ";
cout << endl;
int test5[6] = { NULL,5,4,3,2,1 };
SelectSort(test5, 5);
cout << "SelectSort:";
for (int i = 1; i <= 5; i++)
cout << test5[i] << " ";
cout << endl << endl;
}
void TestC() {
cout << "Random data:" << endl;
int test1[6] = { NULL,20,45,3,5,9 };
cout << "The test array is:";
for (int i = 1; i <= 5; i++)
cout << test1[i] << ' ';
cout << endl;
InsertSort(test1, 5);
cout << "InnsertSort:";
for (int i = 1; i <= 5; i++)
cout << test1[i] << " ";
cout << endl;
int test2[6] = { NULL,20,45,3,5,9 };
ShellInsert(test2, 5);
cout << "ShellInsert:";
for (int i = 1; i <= 5; i++)
cout << test2[i] << " ";
cout << endl;
int test3[6] = { NULL,20,45,3,5,9 };
BubbleSort(test3, 5);
cout << "BubbleSort:";
for (int i = 1; i <= 5; i++)
cout << test3[i] << " ";
cout << endl;
int test4[6] = { NULL,20,45,3,5,9 };
QuickSort(test4, 1, 5);
cout << "QuickSort:";
for (int i = 1; i <= 5; i++)
cout << test4[i] << " ";
cout << endl;
int test5[6] = { NULL,20,45,3,5,9 };
SelectSort(test5, 5);
cout << "SelectSort:";
for (int i = 1; i <= 5; i++)
cout << test5[i] << " ";
cout << endl<<endl;
}
int main() {
TestA();
TestB();
TestC();
return 0;
}
测试结果: