参考https://www.cnblogs.com/onepixel/articles/7674659.html
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print_vec(string str, vector<int> &origin, vector<int> vec) {
cout << "---------- algorithms: " << str << " -------------" << endl;
cout << "origin: ";
for (auto& v : origin) {
cout << v << " ";
}
cout << endl << "sorted: ";
for (auto& v : vec) {
cout << v << " ";
}
cout << endl;
}
class my_sort {
public:
// 冒泡排序 -------------------------------------
vector<int> bubble_sort(vector<int> &vec) {
int size = vec.size();
vector<int> res(vec);
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (res[j] > res[j + 1])
swap(res[j], res[j + 1]);
}
}
return res;
}
// 选择排序 -------------------------------------
vector<int> select_sort(vector<int>& vec) {
int size = vec.size();
vector<int> res(vec);
int minIndex;
for (int i = 0; i < size - 1; i++) {
minIndex = i;
for (int j = i + 1; j < size; j++) {
if (res[j] < res[minIndex])
minIndex = j;
}
swap(res[i], res[minIndex]);
}
return res;
}
// 插入排序 -------------------------------------
vector<int> insert_sort(vector<int>& vec) {
int size = vec.size();
vector<int> res(vec);
for (int i = 1; i < size; i++) {
int index = i;
int current = res[i];
while (index > 0 && res[index - 1] > current) {
res[index] = res[index - 1];
index--;
}
res[index] = current;
}
return res;
}
// 希尔排序 -------------------------------------
vector<int> shell_sort(vector<int>& vec) {
int size = vec.size();
vector<int> res(vec);
for (int gap = size / 2; gap > 0; gap /= 2) {
for (int i = gap; i < size; i++) {
int j = i;
int current = res[i];
while (j - gap >= 0 && current < res[j - gap]) {
res[j] = res[j - gap];
j -= gap;
}
res[j] = current;
}
}
return res;
}
// 归并排序 -------------------------------------
void merge(vector<int>& vec, int beg, int mid, int end) {
vector<int> tmp(end - beg + 1);
for (int i = 0; i <= end - beg; i++) {
tmp[i] = vec[beg + i];
}
int l = 0, r = mid+1-beg;
for (int i = 0; i <= end - beg; i++) {
if (l <= mid-beg && r <= end-beg) {
vec[beg + i] = tmp[l] <= tmp[r] ? tmp[l++] : tmp[r++];
} else if (l > mid-beg) {
vec[beg + i] = tmp[r++];
} else {
vec[beg + i] = tmp[l++];
}
}
}
void merge_pocess(vector<int> &vec, int beg, int end) {
if (end - beg < 1) return;
int mid = (end + beg) / 2;
merge_pocess(vec, beg, mid);
merge_pocess(vec, mid+1, end);
merge(vec, beg, mid, end);
}
vector<int> merge_sort(vector<int>& vec) {
int size = vec.size();
vector<int> res(vec);
merge_pocess(res, 0, size - 1);
return res;
}
// 快速排序 -------------------------------------
void quick_pocess(vector<int>& vec, int left, int right) {
if (left < right) {
int i = left + 1, j = right;
while (i <= j) {
while (i <= j && vec[left] > vec[i]) i++;
while (i <= j && vec[left] < vec[j]) j--;
if (i >= j) break;
swap(vec[i], vec[j]);
}
swap(vec[left], vec[j]);
quick_pocess(vec, left, j-1);
quick_pocess(vec, j+1, right);
}
}
vector<int> quick_sort(vector<int>& vec) {
int size = vec.size();
vector<int> res(vec);
quick_pocess(res, 0, size - 1);
return res;
}
// 堆排序 -------------------------------------
void adjust(vector<int>& vec, int size, int index) {
int left = 2 * index + 1;
int right = 2 * index + 2;
int maxIndex = index;
if (left < size && vec[left] > vec[maxIndex]) maxIndex = left;
if (right < size && vec[right] > vec[maxIndex]) maxIndex = right;
if (maxIndex != index) {
swap(vec[maxIndex], vec[index]);
adjust(vec, size, maxIndex);
}
}
vector<int> heap_sort(vector<int>& vec) {
int size = vec.size();
vector<int> res(vec);
for (int i = size / 2 - 1; i >= 0; i--) {
adjust(res, size, i);
}
for (int i = size - 1; i >= 1; i--) {
swap(res[0], res[i]);
adjust(res, i, 0);
}
return res;
}
// 计数排序 -------------------------------------
vector<int> count_sort(vector<int>& vec) {
int size = vec.size();
int maxNum = vec[0];
vector<int> res(vec);
for (auto& t : vec)
maxNum = max(maxNum, t);
vector<int> bucket(maxNum + 1, 0);
for (auto& t : vec) {
bucket[t]++;
}
int j = 0;
for (int i = 0; i < bucket.size();i++) {
while (bucket[i]--)
res[j++] = i;
}
return res;
}
// 桶排序 -------------------------------------
vector<int> bucket_sort(vector<int>& vec) {
int size = vec.size();
vector<int> res(vec);
int BUCKET_NUM = 5;
int minNum = res[0], maxNum = res[0];
for (auto& t : res) {
minNum = min(minNum, t);
maxNum = max(maxNum, t);
}
vector<vector<int>> bucket(BUCKET_NUM);
int bucketSize = (maxNum - minNum) / BUCKET_NUM + 1;
for (auto& t : res) {
bucket[(t - minNum) / bucketSize].push_back(t);
}
for (auto& v : bucket)
sort(v.begin(), v.end());
int i = 0;
for (auto& v : bucket) {
for (auto& t : v) {
cout << t << " ";
res[i++] = t;
}
}
cout << endl;
return res;
}
// 基数排序 -------------------------------------
vector<int> radix_sort(vector<int>& vec) {
int size = vec.size();
vector<int> res(vec);
int BUCKETS = 10;
int N = 1; //多少位数
vector<vector<int>> bucket(BUCKETS);
for (int i = 0; i <= N; i++) {
int mod = static_cast<int>(pow(10, i));
vector<vector<int>> bucket(BUCKETS);
for (auto& t : res) {
bucket[t / mod % 10].push_back(t);
}
int j = 0;
for (auto& v : bucket) {
for (auto& t : v) {
res[j++] = t;
}
}
}
return res;
}
private:
void swap(int& a, int& b) {
int tmp = a;
a = b;
b = tmp;
}
};
int main() {
vector<int> temp = { 5,4,1,2,3,7,8,9,0,6 };
my_sort sort_obj;
print_vec("冒泡排序", temp, sort_obj.bubble_sort(temp));
print_vec("选择排序", temp, sort_obj.select_sort(temp));
print_vec("插入排序", temp, sort_obj.insert_sort(temp));
print_vec("希尔排序", temp, sort_obj.shell_sort(temp));
print_vec("归并排序", temp, sort_obj.merge_sort(temp));
print_vec("快速排序", temp, sort_obj.quick_sort(temp));
print_vec("堆排序", temp, sort_obj.heap_sort(temp));
print_vec("计数排序", temp, sort_obj.count_sort(temp));
print_vec("桶排序", temp, sort_obj.bucket_sort(temp));
print_vec("基数排序", temp, sort_obj.radix_sort(temp));
return 0;
}