标签:希尔 clu code name 运行时 vector ios ret return
1、选择排序
#include <vector> #include <iostream> #include <random> #include <algorithm> using namespace std; void SelectionSort(vector<int> &data) { for (int i = 0; i < data.size(); ++i) { int tmp = i; for (int j = i + 1; j < data.size(); ++j) { if (data[j] < data[tmp]) { tmp = j; } } if (tmp != i) swap(data[tmp], data[i]); } } int main(int argc, char *argv[]) { vector<int> data1 = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; vector<int> data2; default_random_engine dre; uniform_int_distribution<int> di(-100, 100); for (int i = 0; i < 50; ++i) data2.push_back(di(dre)); SelectionSort(data1); for (auto i : data1) cout << i << " "; cout << endl; SelectionSort(data2); for (auto i : data2) cout << i << " "; cout << endl; return 0; }
2、冒泡排序
#include <vector> #include <iostream> #include <random> #include <algorithm> using namespace std; void BubbleSort(vector<int> &data) { for (int i = 0; i < data.size(); ++i) { for (int j = data.size() - 1; j > i; --j) { if (data[j] < data[j - 1]) swap(data[j], data[j - 1]); } } } int main(int argc, char *argv[]) { vector<int> data1 = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; vector<int> data2; default_random_engine dre; uniform_int_distribution<int> di(-100, 100); for (int i = 0; i < 50; ++i) data2.push_back(di(dre)); BubbleSort(data1); for (auto i : data1) cout << i << " "; cout << endl; BubbleSort(data2); for (auto i : data2) cout << i << " "; cout << endl; return 0; }
3、插入排序
#include <vector> #include <iostream> #include <random> #include <algorithm> using namespace std; void InsertionSort(vector<int> &data) { for (int i = 1; i < data.size(); ++i) { int tmp = data[i]; int j = i - 1; while (j >= 0 && data[j] > tmp) { data[j + 1] = data[j]; --j; } data[j + 1] = tmp; } } int main(int argc, char *argv[]) { vector<int> data1 = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; vector<int> data2; default_random_engine dre; uniform_int_distribution<int> di(-100, 100); for (int i = 0; i < 50; ++i) data2.push_back(di(dre)); InsertionSort(data1); for (auto i : data1) cout << i << " "; cout << endl; InsertionSort(data2); for (auto i : data2) cout << i << " "; cout << endl; return 0; }
4、希尔排序
希尔排序的运行时间与增量序列的选择相关。下面程序增量序列的选择时间复杂度为O(n3/2)。
#include <vector> #include <iostream> #include <random> #include <algorithm> using namespace std; void ShellSort(vector<int> &data) { int gap = 1; while (gap < data.size()) gap = gap * 3 + 1; for (; gap > 0; gap /= 3) { for (int i = gap; i < data.size(); ++i) { int tmp = data[i]; int j = i - gap; while (j >= 0 && data[j] > tmp) { data[j + gap] = data[j]; j -= gap; } data[j + gap] = tmp; } } } int main(int argc, char *argv[]) { vector<int> data1 = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; vector<int> data2; default_random_engine dre; uniform_int_distribution<int> di(-100, 100); for (int i = 0; i < 50; ++i) data2.push_back(di(dre)); ShellSort(data1); for (auto i : data1) cout << i << " "; cout << endl; ShellSort(data2); for (auto i : data2) cout << i << " "; cout << endl; return 0; }
标签:希尔 clu code name 运行时 vector ios ret return
原文地址:http://www.cnblogs.com/sgkind/p/6357731.html