跟着B站的黑马程序员学习数据结构(36_排序_快速排序5_哔哩哔哩_bilibili),其中前几节介绍的是利用数组排序的方法,但他里面用到的java,我在这边把把java代码转换成C++代码。以下列出的排序方法分别有冒泡排序,选择排序,插入排序,希尔排序,归并排序,快速排序。
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
using namespace std;
void sortArr(int arr[], int low, int hight);
void QuickSort(int arr[], int low, int hight);
int main()
{
//冒泡排序
int n1 = 8;
int t = 0;
int a[n1] = {8, 7, 6, 5, 4, 3, 2, 1};
for (int i = n1 - 1; i > 0; i--) //目的是把最大的数字放最右边
{
for (int j = 0; j < i; j++) //只需要比较i前面那几个,因为我们是从右到左排序
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
for (int i = 0; i < n1; i++)
{
cout << a[i] << " ";
}
cout << endl
<< "---------------------------------------"
<< endl;
//选择排序
int n2 = 8;
int b[n2] = {8, 7, 6, 5, 4, 3, 2, 1};
for (int i = 0; i < n2 - 1; i++) //找到最小值的索引 把最小值与元素最左边值交换,然后层层叠进
{
int minIndex = i;
for (int j = i + 1; j < n2; j++)
{
if (b[minIndex] < b[j])
{
minIndex = j;
}
swap(b[minIndex], b[j]);
}
}
for (int i = 0; i < n2; i++)
{
cout << b[i] << " ";
}
cout << endl
<< "---------------------------------------"
<< endl;
//插入排序
int n3 = 8;
int c[n3] = {8, 7, 6, 5, 4, 3, 2, 1};
for (int i = 1; i < n3; i++) // 类似扑克牌,看到较小值就把他移到前面去
{
for (int j = i; j > 0; j--)
{
if (c[j] < c[j - 1])
swap(c[j], c[j - 1]);
else
break;
}
}
for (int i = 0; i < n3; i++)
{
cout << c[i] << " ";
}
cout << endl
<< "---------------------------------------"
<< endl;
//希尔排序
int n4 = 8;
int d[n4] = {9, 5, 4, 29, 8, 7, 2, 3};
// 1.根据数组a的长度,确定增长量h的初始值
int h = 1;
while (h < n4 / 2)
{
h = 2 * h + 1;
}
// 2.希尔排序
while (h >= 1)
{
//排序
// 2.1找到待插入的元素
for (int i = h; i < n4; i++)
{
for (int j = i; j >= h; j -= h)
{
if (d[j - h] > d[j])
{
swap(d[j - h], d[j]);
}
else
break;
}
}
h = h / 2;
}
for (int i = 0; i < n3; i++)
{
cout << d[i] << " ";
}
cout << endl
<< "---------------------------------------"
<< endl;
//归并排序
int n5 = 8;
int e[n5] = {9, 5, 4, 29, 8, 7, 2, 3};
sortArr(e, 0, n5 - 1);
for (int i = 0; i < n5; i++)
{
cout << e[i] << " ";
}
cout << endl
<< "---------------------------------------"
<< endl;
//快速排序
int n6 = 8;
int f[n6] = {9, 5, 4, 29, 8, 7, 2, 3};
QuickSort(f, 0, n6 - 1);
for (int i = 0; i < n5; i++)
{
cout << e[i] << " ";
}
}
void mergeArr(int arr[], int low, int mid, int hight) //归并排序中的并
{
int *tempArr = new int[hight - low + 1];
int i = low, j = mid + 1, k = 0; //创建3个指针
while (i <= mid && j <= hight) //通过俩个指针比较俩个数组之间的大小,把小的值先存入数组中
{
if (arr[i] < arr[j])
{
tempArr[k] = arr[i];
i++;
}
else
{
tempArr[k] = arr[j];
j++;
}
k++;
}
while (i <= mid) //如果 arr[low] 到 arr[mid] 区间中的数组还没有比较完成 ,直接复制到tempArr 中
{
tempArr[k] = arr[i];
i++;
k++;
}
while (j <= hight) // 如果 arr[mid+1] 到 arr[hight] 区间中的数组还没有比较完成 ,直接复制到tempArr 中
{
tempArr[k] = arr[j];
j++;
k++;
}
i = low;
for (int t = 0; ((t < k) && (i <= hight)); t++)
{
arr[i] = tempArr[t];
i++;
}
delete[] tempArr;
}
void sortArr(int arr[], int low, int hight)
{
if (low < hight)
{
int mid = (hight + low) / 2;
sortArr(arr, low, mid); //递归拆左边
sortArr(arr, mid + 1, hight); //递归拆右边
mergeArr(arr, low, mid, hight);
}
}
int Partition(int arr[], int low, int hight)
{
int pivot = arr[low]; //定义一个中间值,这边默认数组的第一个数
while (low < hight) //循环终止的条件是左边的指针比右边的指针大,这里的指针直接用low和hight即可
{
while (low < hight && arr[hight] >= pivot) //先走右指针,找到一个比原先定义的中间值的数字大的值放在他右边
hight--;
arr[low] = arr[hight];
while (low < hight && arr[low] <= pivot) //再走左指针,找到一个比原先定义的中间值的数字小的值放在他左边
low++;
arr[hight] = arr[low];
}
arr[low] = pivot;
return low;
}
void QuickSort(int arr[], int low, int hight) //拆分
{
if (low < hight)
{
int pivot = Partition(arr, low, hight);
QuickSort(arr, low, pivot - 1);
QuickSort(arr, pivot + 1, hight);
}
}
/*
*/