九大排序算法(C++实现)
2021/8/26 1:06:13
本文主要是介绍九大排序算法(C++实现),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- Quciksort
- Mergesort
- Insertionsort
- Bubblesort
- Selectionsort
- Shellsort
- Heapsort
- Countsort
- Radixsort
- Summary
Quciksort
void quick_sort(vector<int> &nums, int l, int r) { if (l + 1 >= r) { return; } int first = l, last = r - 1, key = nums[first]; while (first < last) { while (first < last && nums[last] >= key) { --last; } nums[first] = nums[last]; while (first < last && nums[first] <= key) { ++first; } nums[last] = nums[first]; } nums[first] = key; quick_sort(nums, l, first); quick_sort(nums, first + 1, r); }
Mergesort
void merge_sort(vector<int> &nums, int l, int r, vector<int> &temp) { if (l + 1 >= r) { return; } // divide int m = l + (r - l) / 2; merge_sort(nums, l, m, temp); merge_sort(nums, m, r, temp); // conquer int p = l, q = m, i = l; while (p < m || q < r) { if (q >= r || (p < m && nums[p] <= nums[q])) { temp[i++] = nums[p++]; } else { temp[i++] = nums[q++]; } } for (i = l; i < r; ++i) { nums[i] = temp[i]; } }
Insertionsort
void insertion_sort<vector<int> &nums, int n){ for (int i = 0; i < n; ++i) { for (int j = 0; j < nums[j]; ++j) { swap(nums[j], nums[j - 1]); } } }
Bubblesort
void bubble_sort(vector<int> &nums, int n) { bool swapped; for (int i = 1; i < n; ++i) { swapped = false; for (int j = 1; j < n - i + 1; ++j) { if (nums[j] < nums[j - 1]) { swap(nums[j], nums[j - 1]); swapped = true; } } if (!swapped) { break; } } }
Selectionsort
void selection_sort(vector<int> &nums, int n) { int mid; for (int i = 0; i < n - 1; ++i) { mid = i; for (int j = 0; j < n; ++j) { if (nums[j] < nums[mid]) mid = j; } swap(nums[mid], nums[i]); } }
Shellsort
void shellSort(vector<int> &q) { int gap = q.size() / 2; while (gap) { for (int i = gap; i < q.size(); i += gap) { int t = q[i], j; for (j = i - gap; j >= 0; j -= gap) { if (q[j] > t) q[j + gap] = q[j]; else break; } q[j + gap] = t; } gap /= 2; } }
Heapsort
void push_down(vector<int> &heap, int size, int u) { int t = u, left = u * 2, right = u * 2 + 1; if (left <= size && heap[left] > heap[t]) t = left; if (right <= size && heap[right] > heap[t]) t = right; if (u != t) { swap(heap[u], heap[t]); push_down(heap, size, t); } } void push_up(vector<int> &heap, int u) { while (u / 2 && heap[u / 2] < heap[u]) { swap(heap[u / 2], heap[u]); u /= 2; } } void heapSort(vector<int> &q, int n) { int size = n; for (int i = 1; i <= n; i++) push_up(q, i); for (int i = 1; i <= n; i++) { swap(q[1], q[size]); size--; push_down(q, size, 1); } }
Countsort
void countingSort(vector<int> &q, int n) { vector<int> cnt(101, 0); for (int i = 0; i < n; i++) cnt[q[i]]++; for (int i = 0, k = 0; i <= 100; i++) { while (cnt[i]) { q[k++] = i; cnt[i]--; } } }
Radixsort
不超过999
int get(int x, int i) { while (i--) x /= 10; return x % 10; } void radixSort(vector<int> &q, int n) { vector<vector<int>> cnt(10); for (int i = 0; i < 3; i++) { for (int j = 0; j < 10; j++) cnt[j].clear(); for (int j = 0; j < n; j++) cnt[get(q[j], i)].push_back(q[j]); for (int j = 0, k = 0; j < 10; j++) { for (int x : cnt[j]) q[k++] = x; } } }
Summary
Algorithm | Complexity | Auxiliary Space | Stability |
---|---|---|---|
Bubble Sort | \(O(n^2)\) | \(O(1)\) | Yes |
Selection Sort | \(O(n^2)\) | \(O(1)\) | No |
Insertion Sort | \(O(n^2)\) | \(O(1)\) | Yes |
Merge Sort | \(O(n\log{n})\) | \(O(n)\) | Yes |
Qucik Sort | \(O(n\log{n})\) | \(O(n\log{n})\) | No |
Heap Sort | \(O(n\log{n})\) | \(O(1)\) | No |
Shell Sort | \(O(n\log{n})\) | \(O(n\log{n})\) | No |
Count Sort | \(O(n+k)\) | \(O(n+k)\) | Yes |
Bucket Sort | \(O(n+k)\) | \(O(n+k)\) | Yes |
Radix Sort | \(O(n*k)\) | \(O(n+k)\) | Yes |
这篇关于九大排序算法(C++实现)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用
- 2025-01-03混合搜索:用LanceDB实现语义和关键词结合的搜索技术(应用于实际项目)