void select_sort(vector<int> &arr) {
for (int i = 0; i < arr.size(); ++i) {
int min = i;
for (int j = i + 1; j < arr.size(); ++j) {
if (arr[j] <= arr[min]) {
min = j;
}
}
swap(arr[i], arr[min]);
}
}
2.冒泡排序
void bubble_sort1(vector<int> &arr) {
int len = arr.size();
int i, j;
for (int i = 0; i < len - 1; ++i) {
for (j = 0; j < len - 1 - i; j++){
if (arr[j] > arr[j+1]) {
swap(arr[j+1], arr[j]);
}
}
}
}
void swap_xor(vector<int>::value_type &value1, vector<int>::value_type &value2) {
value1 = value1 ^ value2;
value2 = value1 ^ value2;
value1 = value1 ^ value2;
};
void bubble_sort2(vector<int> &arr) {
int len = arr.size();
int i, j;
for (int i = 0; i < len - 1; ++i) {
for (int j = i + 1; j < len; ++j) {
if (arr[j] < arr[i]) {
swap_xor(arr[i], arr[j]);
}
}
}
}
3.插入排序
//
void insert_sort(vector<int> &arr) {
int len = arr.size();
for (int i = 1; i < len; ++i) {
vector<int>::value_type key = arr[i];
int j = i - 1;
while ((j >= 0) && key < arr[j]) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
4.希尔排序 (while)
void shell_sort1(vector<int> &arr) {
int len = arr.size();
int interval = len >> 1; // 获取初始长度
while (interval >= 1) {
for (int i = interval; i < len; ++i) {
vector<int>::value_type tmp = arr[i];
int j = i;
while ((j - interval >= 0) && (arr[j - interval] > tmp)) {
arr[j] = arr[j - interval];
j -= interval;
}
arr[j] = tmp;
}
interval /= 2;
}
}
4.希尔排序 (3 for)
// 4. 希尔排序 3 for (算法导论)
void shell_sort2(vector<int> &arr) {
int gap, i, j;
int len = arr.size();
for (int gap = len / 2; gap > 0; gap /= 2) {
for (int i = gap; i < len; ++i) {
for (int j = i - gap; (j > 0) && (arr[j + gap] < arr[j]); j--) {
swap(arr[j + gap], arr[j]);
}
}
}
}
5.归并排序 (递归)
void merge1(int arr[], int l, int q, int r) {
int n = r - l + 1;
int *tmp = new int[n];
int i = 0;
int left = l;
int right = q + 1;
while (left <= q && right <= r) {
tmp[i++] = arr[left] <= arr[right] ? arr[left++] : arr[right++];
}
while (left <= q) {
tmp[i++] = arr[left++];
}
while (right <= r) {
tmp[i++] = arr[right++];
}
copy(tmp, tmp + n, arr + l);
delete[] tmp;
}
void merge_sort1(int *arr, int l, int r) {
if (l == r) {
return;
}
int q = (l + r) >> 1;
merge_sort1(arr, l, q);
merge_sort1(arr, q + 1, r);
merge1(arr, l, q, r);
}
5.归并排序 (迭代)
void merge2(vector<int> &arr2, int head1, int tail1, int tail2) {
int left1 = head1, len = tail2 - head1 + 1, left2 = tail1 + 1, index = 0;
vector<int> tmp(len);
while (left1 <= tail1 && left2 <= tail2) {
if (arr2[left1] < arr2[left2]) {
tmp[index++] = arr2[left1++];
} else {
tmp[index++] = arr2[left2++];
}
}
while (left1 <= tail1) {
tmp[index++] = arr2[left1++];
}
while (left2 <= tail2) {
tmp[index++] = arr2[left2++];
}
copy(tmp.begin(), tmp.end(), arr2.begin() + head1);
}
void merge_sort2(vector<int> &arr) {
int len = arr.size();
for (int step = 1; step < len; step += step) {
for (int index = 0; index < len; index += step + step) {
merge2(arr, index, min(index + step - 1, len - 1), min(index + step + step - 1, len - 1));
}
}
}
6.快速排序
void quick_sort(vector<int> &arr, vector<int>::iterator left, vector<int>::iterator right) {
if (left > right) {
return;
}
vector<int>::value_type tmp = (*left);
auto i = left;
auto j = right;
while (i != j) {
while ((*j) >= tmp && j > i) {
j--;
}
while ((*i) <= tmp && j > i) {
i++;
}
if (j > i) {
swap((*j), (*i));
}
}
*left = *i;
*i = tmp;
quick_sort(arr, left, i - 1);
quick_sort(arr, i + 1, right);
}
7.记数排序
void count_sort(vector<int> &arr) {
int len = arr.size();
vector<int>::value_type maxx = *max_element(arr.begin(), arr.end());
vector<int>::value_type minx = *min_element(arr.begin(), arr.end());
vector<int> tmp(maxx - minx + 1, 0);
vector<int> ans;
for (int i = 0; i < len; ++i) {
tmp[arr[i] - minx]++;
}
for (int j = 0; j < maxx - minx + 1; ++j) {
while (tmp[j] != 0) {
ans.push_back(j + minx);
tmp[j]--;
}
}
copy(ans.begin(), ans.end(), arr.begin());
}
8. 桶排序
void bucket_sort(int *arr, int n) {
vector<vector<int>> bucket(10); // 10个桶
for (int i = 0; i < 10; ++i) {
vector<int> x{0};
bucket.push_back(x);
}
for (int i = 0; i < n; ++i) {
bucket[arr[i] / 10].push_back(arr[i]);
}
int index = 0;
for (int i = 0; i < 10; ++i) {
sort(bucket[i].begin(), bucket[i].end());
// sort of bucket
for (auto it = bucket[i].begin(); it != bucket[i].end(); ++it) {
arr[index++] = *it;
}
}
}
9.基数排序
// 9.
void radix_sort(vector<int> &arr, int n) { // n 为位数
const int BUCKETS = 10;
vector<vector<int>> buckets(BUCKETS);
for (int poss = 0; poss <= n - 1; ++poss) {
int denominator = static_cast<int> (pow(10, poss));
for (int &tmp: arr) {
buckets[(tmp / denominator) % 10].push_back(tmp);
}
int index = 0;
for (auto &thebuckett: buckets) {
for (int &k: thebuckett) {
arr[index++] = k;
thebuckett.clear();
}
}
}
}
10.堆排序
namespace heap {
void drwn(int *arr, int i, int n) {
int parent = i;
int child = 2 * i + 1;// 左孩子
while (child < n) {
if (child + 1 < n && arr[child] < arr[child + 1]) {
child++;
}
if (arr[parent] < arr[child]) {
swap(arr[parent], arr[child]);
parent = child;
}
child = child * 2 + 1;
}
}
void build_heap(int *arr, int size) {
for (int i = 2 * size - 1; i >= 0; i--) {
drwn(arr, i, size);
}
}
void heap_sort(int *arr, int size) {
build_heap(arr, size);
for (int i = size - 1; i > 0; i--) {
swap(arr[i], arr[0]);
drwn(arr, 0, i);
}
}
}