插入、希尔、冒泡、快速、选择排序C++实现
#include
using namespace std;
void printRes(int a[], int n);
void insertSort(int a[], int n, bool flag);
void shellSort(int a[], int n, bool flag);
void bubbleSort(int a[], int n, bool flag);
void fastSort(int a[], int nl, int nr, bool flag);
void selectSort(int a[], int n, bool flag);
int main()
{
int a[10] = { 51,26,56,89,23,-60,2,9,100,78 };
cout << "排序前:" << endl;
printRes(a,10);
cout << "顺序插入排序后:" << endl;
insertSort(a, 10, true);
printRes(a,10);
cout << "逆序插入排序后:" << endl;
insertSort(a, 10, false);
printRes(a, 10);
cout << "顺序希尔排序后:" << endl;
shellSort(a, 10, true);
printRes(a, 10);
cout << "逆序希尔排序后:" << endl;
shellSort(a, 10, false);
printRes(a, 10);
cout << "顺序冒泡排序后:" << endl;
bubbleSort(a, 10, true);
printRes(a, 10);
cout << "逆序冒泡排序后:" << endl;
bubbleSort(a, 10, false);
printRes(a, 10);
cout << "顺序快速排序后:" << endl;
fastSort(a, 0,9, true);
printRes(a, 10);
cout << "逆序快速排序后:" << endl;
fastSort(a, 0,9, false);
printRes(a, 10);
cout << "顺序选择排序后:" << endl;
selectSort(a, 10, true);
printRes(a, 10);
cout << "逆序选择排序后:" << endl;
selectSort(a, 10, false);
printRes(a, 10);
}
void shellSort(int a[], int n,bool flag)
{
if (flag)
{
for (size_t step = n/2; step > 0; step = step /2)
{
for (size_t i = 0; i < step; i++)
{
for (size_t j = i+ step; j < n; j+= step)
{
int temp = a[j];
int k = j - step;
while (k>=0&&(a[k]>temp))
{
a[k + step] = a[k];
k = k - step;
}
a[k + step] = temp;
}
}
}
}
else
{
for (size_t step = n / 2; step > 0; step = step / 2)
{
for (size_t i = 0; i < step; i++)
{
for (size_t j = i + step; j < n; j += step)
{
int temp = a[j];
int k = j - step;
while (k >= 0 && (a[k] < temp))
{
a[k + step] = a[k];
k = k - step;
}
a[k + step] = temp;
}
}
}
}
}
void insertSort(int a[], int n, bool flag)
{
if (flag)
{
for (size_t i = 1; i < n; i++)
{
int temp = a[i];
int j = i - 1;
while (j >= 0 && (a[j] > temp))
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
}
else
{
for (size_t i = 1; i < 10; i++)
{
int temp = a[i];
int j = i - 1;
while (j >= 0 && (a[j] < temp))
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
}
}
void bubbleSort(int a[], int n, bool flag)
{
if (flag)
{
for (size_t i = 0; i < n; i++)
{
for (size_t j = i + 1; j < n; j++)
{
int temp = a[i];
if (a[j] < a[i])
{
a[i] = a[j];
a[j] = temp;
}
}
}
}
else
{
for (size_t i = 0; i < n; i++)
{
for (size_t j = i + 1; j < n; j++)
{
int temp = a[i];
if (a[j] > a[i])
{
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
void fastSort(int a[], int nl,int nr, bool flag)
{
if (flag)
{
int l = nl;
int r = nr;
int temp = a[l];
bool rightOrleftFlag = true;
if (nl<nr)
{
while (l < r)
{
if (rightOrleftFlag)
{
if (a[r] >= temp)
{
r--;
continue;
}
a[l] = a[r];
l++;
rightOrleftFlag = false;
continue;
}
else
{
if (a[l] <= temp)
{
l++;
continue;
}
a[r] = a[l];
r--;
rightOrleftFlag = true;
continue;
}
}
a[l] = temp;
fastSort(a, nl, l, true);
fastSort(a, l + 1, nr, true);
}
}
else
{
int l = nl;
int r = nr;
int temp = a[l];
bool rightOrleftFlag = true;
if (nl < nr)
{
while (l < r)
{
if (rightOrleftFlag)
{
if (a[r] <= temp)
{
r--;
continue;
}
a[l] = a[r];
l++;
rightOrleftFlag = false;
continue;
}
else
{
if (a[l] >= temp)
{
l++;
continue;
}
a[r] = a[l];
r--;
rightOrleftFlag = true;
continue;
}
}
a[l] = temp;
fastSort(a, nl, l, false);
fastSort(a, l + 1, nr, false);
}
}
}
void selectSort(int a[], int n, bool flag)
{
if (flag)
{
for (size_t i = 0; i < n - 1; i++)
{
int location = i;
for (size_t j = i + 1; j < n; j++)
{
if (a[j] < a[location])
{
location = j;
a[location] = a[j];
}
}
if (i != location)
{
int temp = a[i];
a[i] = a[location];
a[location] = temp;
}
}
}
else
{
for (size_t i = 0; i < n - 1; i++)
{
int location = i;
for (size_t j = i + 1; j < n; j++)
{
if (a[j] > a[location])
{
location = j;
a[location] = a[j];
}
}
if (i != location)
{
int temp = a[i];
a[i] = a[location];
a[location] = temp;
}
}
}
}
void printRes(int a[], int n)
{
for (size_t i = 0; i < n; i++)
{
cout <<i<<":"<< a[i] << endl;
}
}