排序算法
Posted acgame
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了排序算法相关的知识,希望对你有一定的参考价值。
几种排序算法的时间复杂度:
1、插入排序
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 5 const int maxn = 10005; 6 int arr[maxn]; 7 int n; 8 9 void insertion_sort() 10 { 11 for (int i = 1; i < n; ++i) 12 { 13 int key = arr[i]; 14 int j = i; 15 while (j - 1 >= 0 && arr[j - 1] > key) 16 { 17 arr[j] = arr[j - 1]; 18 --j; 19 } 20 arr[j] = key; 21 } 22 } 23 void print_arr(const int *arr, int n) 24 { 25 for (int i = 0; i < n; ++i) 26 std::cout << arr[i] << " "; 27 std::cout << std::endl; 28 } 29 int main() 30 { 31 std::ifstream is("in.txt"); 32 while (is >> arr[n++]); 33 --n; 34 print_arr(arr, n); 35 insertion_sort(); 36 print_arr(arr, n); 37 return 0; 38 }
2、归并排序
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 5 const int maxn = 10005; 6 int arr[maxn], L[maxn], R[maxn]; 7 int n; 8 9 void merge(int *arr, int l, int r) 10 { 11 int mid = (l + r) / 2; 12 int n1 = mid - l + 1; 13 int n2 = r - mid; 14 for (int i = 0; i < n1; ++i) 15 L[i] = arr[l + i]; 16 for (int i = 0; i < n2; ++i) 17 R[i] = arr[mid + i + 1]; 18 int i = 0, j = 0; 19 while (i < n1 && j < n2) 20 { 21 if (L[i] <= R[j]) 22 arr[l++] = L[i++]; 23 else 24 arr[l++] = R[j++]; 25 } 26 while (i < n1) 27 arr[l++] = L[i++]; 28 while (j < n2) 29 arr[l++] = R[j++]; 30 } 31 32 void merge_sort(int *arr, int l, int r) 33 { 34 if (l == r) 35 return; 36 int mid = (l + r) / 2; 37 merge_sort(arr, l, mid); 38 merge_sort(arr, mid + 1, r); 39 merge(arr, l, r); 40 } 41 42 void print_arr(const int *arr, int n) 43 { 44 for (int i = 0; i < n; ++i) 45 std::cout << arr[i] << " "; 46 std::cout << std::endl; 47 } 48 int main() 49 { 50 std::ifstream is("in.txt"); 51 while (is >> arr[n++]); 52 --n; 53 print_arr(arr, n); 54 merge_sort(arr, 0, n - 1); 55 print_arr(arr, n); 56 return 0; 57 }
3、堆排序
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 5 template<typename T> 6 void swap(T &x, T &y) { 7 T temp = x; 8 x = y; 9 y = temp; 10 } 11 12 const int maxn = 10005; 13 int arr[maxn]; 14 int n; 15 16 17 void max_heapinf(int *arr, int i, int length) // 维护堆的性质 18 { 19 int l = 2 * i, r = l + 1; 20 int largest = i; 21 if (l <= length && arr[l] > arr[largest]) 22 largest = l; 23 if (r <= length && arr[r] > arr[largest]) 24 largest = r; 25 if (largest != i) 26 { 27 swap(arr[largest], arr[i]); 28 max_heapinf(arr, largest, length); 29 } 30 } 31 32 void build_max_heap(int *arr) // 建堆 33 { 34 int length = n; 35 for (int i = n / 2; i >= 1; i--) 36 max_heapinf(arr, i, length); 37 } 38 39 void heapsort(int *arr) 40 { 41 build_max_heap(arr); 42 int length = n; 43 for (int i = n; i >= 2; --i) 44 { 45 swap(arr[i], arr[1]); 46 --length; 47 max_heapinf(arr, 1, length); 48 } 49 } 50 51 void print_arr(const int *arr,int l, int r) 52 { 53 for (int i = l; i <= r; ++i) 54 std::cout << arr[i] << " "; 55 std::cout << std::endl; 56 } 57 int main() 58 { 59 std::ifstream is("in.txt"); 60 while (is >> arr[++n]); 61 --n; 62 print_arr(arr, 1, n); 63 heapsort(arr); 64 print_arr(arr, 1, n); 65 return 0; 66 }
4、快速排序
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 5 template<typename T> 6 void swap(T &x, T &y) { 7 T temp = x; 8 x = y; 9 y = temp; 10 } 11 12 const int maxn = 10005; 13 int arr[maxn]; 14 int n; 15 16 17 int partition(int *arr, int l, int r) // [l, r] 18 { 19 int key = arr[r]; 20 int pos = l; 21 for (int j = l; j < r; ++j) 22 { 23 if (arr[j] <= key) 24 { 25 if (pos != j) 26 swap(arr[pos], arr[j]); 27 ++pos; 28 } 29 } 30 if (pos != r) 31 swap(arr[pos], arr[r]); 32 return pos; 33 } 34 35 void quick_sort(int *arr, int l, int r) 36 { 37 if (l >= r) 38 return; 39 int mid = partition(arr, l, r); 40 quick_sort(arr, l, mid - 1); 41 quick_sort(arr, mid + 1, r); 42 } 43 44 void print_arr(const int *arr, int l, int r) 45 { 46 for (int i = l; i <= r; ++i) 47 std::cout << arr[i] << " "; 48 std::cout << std::endl; 49 } 50 int main() 51 { 52 std::ifstream is("in.txt"); 53 while (is >> arr[++n]); 54 --n; 55 print_arr(arr, 1, n); 56 quick_sort(arr, 1, n); 57 print_arr(arr, 1, n); 58 return 0; 59 }
5、计数排序
计数排序假设n个输入元素中的每一个都是在0到k区间的一个整数,其中k为某个整数。
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 5 template<typename T> 6 T max(T &x, T &y) { 7 return x >= y ? x : y; 8 } 9 template<typename T> 10 void swap(T &x, T &y) { 11 T temp = x; 12 x = y; 13 y = temp; 14 } 15 16 const int maxn = 10005; 17 int arr[maxn]; // 输入序列 18 int b[maxn]; // 输出序列 19 int c[maxn]; // 计数用 20 21 void counting_sort(int *arr, int l, int r) 22 { 23 int k = 0; 24 for (int i = l; i <= r; ++i) 25 { 26 ++c[arr[i]]; 27 k = max(k, arr[i]); 28 } 29 for (int i = 1; i <= k; ++i) 30 c[i] += c[i - 1]; 31 for (int i = r; i >= l; --i) 32 { 33 b[c[arr[i]]--] = arr[i]; 34 } 35 } 36 37 void print_arr(const int *arr, int l, int r) 38 { 39 for (int i = l; i <= r; ++i) 40 std::cout << arr[i] << " "; 41 std::cout << std::endl; 42 } 43 int main() 44 { 45 int n = 0; 46 std::ifstream is("in.txt"); 47 while (is >> arr[++n]); 48 --n; 49 print_arr(arr, 1, n); 50 counting_sort(arr, 1, n); 51 print_arr(b, 1, n); 52 return 0; 53 }
6、基数排序
7、桶排序
以上是关于排序算法的主要内容,如果未能解决你的问题,请参考以下文章