添加插入、堆、归并、希尔、计数、桶、基数排序算法及其实现

This commit is contained in:
huihut
2018-04-16 13:30:03 +08:00
parent 90ef525b75
commit 3a0079899d
11 changed files with 380 additions and 21 deletions

View File

@@ -24,4 +24,19 @@ void bubble_sort(T arr[], int len) {
for (int j = 0; j < len - 1 - i; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
// 冒泡排序(改进版)
void BubbleSort_orderly(vector<int>& v) {
int len = v.size();
bool orderly = false;
for (int i = 0; i < len - 1 && !orderly; ++i) {
orderly = true;
for (int j = 0; j < len - 1 - i; ++j) {
if (v[j] > v[j + 1]) { // 从小到大
orderly = false; // 发生交换则仍非有序
swap(v[j], v[j + 1]);
}
}
}
}