交换类排序
冒泡排序(起泡排序)
大的数据不断往后移动,小的数据像气泡一样往上冒。
如果在一趟排序中没有发生值的交换了,冒泡排序就可以结束了。
空间复杂度
O
(
1
)
O(1)
O(1),平均时间复杂度
O
(
n
2
)
O(n^2)
O(n2),最好时间复杂度
O
(
n
)
O(n)
O(n)。
冒泡排序是稳定算法。
#include <iostream>
using namespace std;
template <typename T>
void BobbleSort(T myarray[], int length)
{
if (length <= 1)
{
return;
}
for (int i = 0; i < length - 1; i++)
{
bool ismoved = false;
for (int j = 0; j < length - i - 1 ; j++)
{
if (myarray[j] > myarray[j + 1])
{
T temp = myarray[j];
myarray[j] = myarray[j + 1];
myarray[j + 1] = temp;
ismoved = true;
}
}
cout << "第" << i + 1 << "趟冒泡排序的结果是:";
for (int j = 0; j < length; j++)
{
cout << myarray[j] << " ";
}
cout << endl;
if (ismoved == false)
{
break;
}
ismoved = false;
}
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int brr[] = { 10,9,8,7,6,5,4,3,2,1 };
int length = sizeof(arr) / sizeof(arr[0]);
BobbleSort(arr, length);
cout << endl;
int length1 = sizeof(brr) / sizeof(brr[0]);
BobbleSort(brr, length1);
}
快速排序(qsort)是对冒泡排序的一种改进
任意一个元素作为枢轴(基准元素)。引入两个指针,low ,high。
#include <iostream>
#include <assert.h>
using namespace std;
template <typename T>
int Partition(T myarray[], int low, int high)
{
static int icount = 0;
icount++;
cout << "【当前Partition调用次数是:" << icount << "】" << endl;
T pivot = myarray[low];
while (low < high)
{
while (low < high && myarray[high] >= pivot)
{
high--;
}
if (low == high)
{
break;
}
if (low < high)
{
myarray[low] = myarray[high];
myarray[high] = pivot;
}
while (low < high && myarray[low] <= pivot)
{
low++;
}
if (low == high)
{
break;
}
if (low < high)
{
myarray[high] = myarray[low];
myarray[low] = pivot;
}
}
return low;
}
template <typename T>
void QuickSort(T myarray[], int low, int high, int length, int lvl = 1)
{
assert(low < high);
cout << "【当前调用深度是:" << lvl << "层】";
int pivotpos = Partition(myarray, low, high);
cout << "low=" << low << "high=" << high << ";枢轴位置=" << pivotpos << "本趟快排结果为:";
for (int i = 0; i < length; i++)
{
cout << myarray[i] << " ";
}
cout << endl;
if (low < pivotpos - 1)
{
QuickSort(myarray, low, pivotpos - 1, length, lvl + 1);
}
if (pivotpos + 1 < high)
{
QuickSort(myarray, pivotpos + 1, high, length, lvl + 1);
}
return;
}
template <typename T>
void QuickSort(T myarray[], int length)
{
if (length <= 1)
{
return;
}
int low = 0;
int high = length - 1;
QuickSort(myarray, low, high, length);
}
int main()
{
int arr[] = { 16,1,45,23,99,2,18,67,42,10 };
int length = sizeof(arr) / sizeof(arr[0]);
QuickSort(arr, length);
cout << endl;
cout << "快速排序最后结果为:";
for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
}
return 0;
}
快速排序效率分析
时间复杂度=
O
(
n
∗
递归调用深度
)
O(n*递归调用深度)
O(n∗递归调用深度)。
空间复杂度=
O
(
递归调用深度
)
O(递归调用深度)
O(递归调用深度)。
递归调用深度:
[
l
o
g
n
,
n
]
[logn,n]
[logn,n]
平均时间复杂度=
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)
平均空间复杂度=
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)
快速排序是不稳定排序算法。