文章目录
- 前言
- 排序算法
- 1.冒泡排序(Bubble Sort) O ( n 2 ) O(n^{2}) O(n2) O ( n 2 ) O(n^{2}) O(n2) O ( n ) O(n) O(n)
- 2. 选择排序(Selection Sort) O ( n 2 ) O(n^{2}) O(n2) O ( n 2 ) O(n^{2}) O(n2) O ( n 2 ) O(n^{2}) O(n2)
- 3. 插入排序(Insertion Sort) O ( n 2 ) O(n^{2}) O(n2) O ( n 2 ) O(n^{2}) O(n2) O ( n ) O(n) O(n)
- 4. 希尔排序(Shell Sort) O ( n 1.3 ) O(n^{1.3}) O(n1.3) O ( n 2 ) O(n^{2}) O(n2) O ( n ) O(n) O(n)
- 5. 快速排序(Quick Sort) O ( n l o g n ) O(nlogn) O(nlogn) O ( n 2 ) O(n^{2}) O(n2) O ( n l o g n ) O(nlogn) O(nlogn)
- 6. 归并排序(Merge Sort) O ( n l o g n ) O(nlogn) O(nlogn) O ( n l o g n ) O(nlogn) O(nlogn) O ( n l o g n ) O(nlogn) O(nlogn)
- 7. 堆排序(Heap Sort) O ( n l o g n ) O(nlogn) O(nlogn) O ( n l o g n ) O(nlogn) O(nlogn) O ( n l o g n ) O(nlogn) O(nlogn)
- 8.计数排序(Counting Sort) O ( n + k ) O(n + k) O(n+k) O ( n + k ) O(n + k) O(n+k) O ( n + k ) O(n + k) O(n+k)
- 9.桶排序(Bucket Sort) O ( n + k ) O(n + k) O(n+k) O ( n 2 ) O(n^{2}) O(n2) O ( n ) O(n) O(n)
- 总结
前言
C++实现
1.时间复杂度O(n^2)的排序算法:冒泡排序、选择排序、插入排序
2.介于时间复杂度O(n^2)到O(nlogn)的排序算法:希尔排序
3.时间复杂度O(nlogn)的排序算法:快速排序、归并排序、堆排序
----------------------突破了O(nlogn)------------------
4.时间复杂度O(n)的排序算法:计数排序、基数排序 (都是桶排序的特殊方法)
不稳定的:选择排序、希尔排序、快速排序、堆排序
排序算法
时间复杂度顺序:平均、最坏、最好
1.冒泡排序(Bubble Sort) O ( n 2 ) O(n^{2}) O(n2) O ( n 2 ) O(n^{2}) O(n2) O ( n ) O(n) O(n)
void bubble_sort(std::vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n - 1; i++) { //n - 1
for (int j = 0; j < n - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int tmp = nums[j + 1];
nums[j + 1] = nums[j];
nums[j] = tmp;
}
}
}
}
2. 选择排序(Selection Sort) O ( n 2 ) O(n^{2}) O(n2) O ( n 2 ) O(n^{2}) O(n2) O ( n 2 ) O(n^{2}) O(n2)
void selection_sort(vector<int>& nums) {
int minv, tmp;
for (int i = 0; i < nums.size() - 1; i++) {
minv = i; //初始化为无序区开始标志
for (int j = i + 1; j < nums.size(); j++) { //在后面寻找最小的数
if (nums[j] < nums[minv]) minv = j; //最小数的索引
}
tmp = nums[i];
nums[i] = nums[minv];
nums[minv] = tmp; //交换找到的最小数到无序区第一个数
}
}
3. 插入排序(Insertion Sort) O ( n 2 ) O(n^{2}) O(n2) O ( n 2 ) O(n^{2}) O(n2) O ( n ) O(n) O(n)
void insertion_sort(vector<int>& nums) {
int pre, cur;
for (int i = 1; i < nums.size(); i++) {
pre = i - 1;
cur = nums[i]; //当前值
while (pre >= 0 && nums[pre] > cur) { //从pre开始往前找,如果比当前值小或等,且此时没有越界(至少是第一个元素),则找到其插入位置
nums[pre + 1] = nums[pre]; //大的元素都往后移动一位
pre--;
}
nums[pre + 1] = cur;//此时nums[pre]小于等于cur, 而后面元素都大于cur,插入pre后面
}
}
4. 希尔排序(Shell Sort) O ( n 1.3 ) O(n^{1.3}) O(n1.3) O ( n 2 ) O(n^{2}) O(n2) O ( n ) O(n) O(n)
void shell_sort(vector<int>& nums) {
for (int gap = floor(nums.size() / 2); gap > 0; gap = floor(gap / 2)) {
//多个分组交替执行
for (int i = gap; i < nums.size(); i++) {
int j = i, cur = nums[i];
while (j - gap >= 0 && nums[j - gap] > cur){
nums[j] = nums[j - gap];
j = j - gap;
}
nums[j] = cur;
}
}
}
5. 快速排序(Quick Sort) O ( n l o g n ) O(nlogn) O(nlogn) O ( n 2 ) O(n^{2}) O(n2) O ( n l o g n ) O(nlogn) O(nlogn)
void quick_sort(vector<int>& nums, int l, int r){
int n = nums.size();
if (l < r) {
int index = part(nums, l, r); //一趟排序,并返回交换后基数下标
quick_sort(nums, l, index - 1); //递归排序左边数组
quick_sort(nums, index + 1, r); //递归排序右边数组
}
}
int part(vector<int>& nums, int l, int r) {
int p = nums[l]; //数组第一个元素作为基准
int i = l, j = r;
while (i < j) {
while (nums[j] >= p && i < j) j--; //从右往左找第一个小于基准的数
while (nums[i] <= p && i < j) i++; //从左往右找第一个大于基准的数
swap(nums[i], nums[j]); //交换两个数
}
swap(nums[i], nums[l]); //把找到的小于基准的数与基数交换,这样小数就换到基准前面
return i;
}
6. 归并排序(Merge Sort) O ( n l o g n ) O(nlogn) O(nlogn) O ( n l o g n ) O(nlogn) O(nlogn) O ( n l o g n ) O(nlogn) O(nlogn)
void _merge(vector<int>& nums, int l, int mid, int r) {
int n= r - l + 1;
vector<int> res;
int i = l, j = mid + 1;
while (i <= mid && j <= r) {
if (nums[i] <= nums[j]) res.push_back(nums[i]), i++;
else res.push_back(nums[j]), j++;
}
while (i <= mid) res.push_back(nums[i]), i++;
while (j <= r) res.push_back(nums[j]), j++;
for (int i = 0; i < n; i++) nums[l + i] = res[i]; //*** nums = res XXXX
}
void merge_sort(vector<int>& nums, int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
merge_sort(nums, l, mid); //[l, mid]
merge_sort(nums, mid + 1, r); //[mid + 1, r]
_merge(nums, l, mid, r);
}
7. 堆排序(Heap Sort) O ( n l o g n ) O(nlogn) O(nlogn) O ( n l o g n ) O(nlogn) O(nlogn) O ( n l o g n ) O(nlogn) O(nlogn)
int len;
void heapify(vector<int>& nums, int i) {
int left = 2 * i + 1, right = 2 * i + 2, largest = i;
if (left < len && nums[left] > nums[largest]) largest = left;
if (right < len && nums[right] > nums[largest]) largest = right;
if (largest != i) {
swap(nums[i], nums[largest]);
heapify(nums, largest);
}
}
void buildMaxHeap(vector<int>& nums) {
len = nums.size();
for (int i = floor(len / 2); i >= 0; i--) heapify(nums, i);
}
void heap_sort(vector<int>& nums) {
buildMaxHeap(nums);
for (int i = nums.size() - 1; i > 0; i--) {
swap(nums[0], nums[i]);
len--;
heapify(nums, 0);
}
}
8.计数排序(Counting Sort) O ( n + k ) O(n + k) O(n+k) O ( n + k ) O(n + k) O(n+k) O ( n + k ) O(n + k) O(n+k)
void counting_sort(vector<int>& nums, int maxv) {
vector<int> cnt(maxv + 1); //计数0-maxv
int sortedIndex = 0;
for (int i = 0; i < nums.size(); i++) {
if (!cnt[nums[i]]) cnt[nums[i]] = 0;
cnt[nums[i]]++; //计数
}
for (int i = 0; i <= maxv; i++) {
while (cnt[i] > 0) {
nums[sortedIndex++] = i;
cnt[i]--;
}
}
}
9.桶排序(Bucket Sort) O ( n + k ) O(n + k) O(n+k) O ( n 2 ) O(n^{2}) O(n2) O ( n ) O(n) O(n)
void bucket_sort(vector<int>& nums, int bucketSize) {
if (nums.empty()) return;
int i;
int minValue = nums[0], maxValue = nums[0];
for (i = 1; i < nums.size(); i++) {
if (nums[i] < minValue) minValue = nums[i];
else if (nums[i] > maxValue) maxValue = nums[i];
}
//桶的初始化
int bucketCount = floor((maxValue - minValue) / bucketSize) + 1;
vector<vector<int>> buckets(bucketCount, vector<int>(0));
//利用映射函数将数据分配到各个桶中
for (int i = 0; i < nums.size(); i++) buckets[floor((nums[i] - minValue) / bucketSize)].push_back(nums[i]);
nums.resize(0);
for (int i = 0; i < buckets.size(); i++) {
Sort sort_b(buckets[i]); //对每个桶进行排序,这里使用插入排序
sort_b.insertion_sort(buckets[i]);
for (int j = 0; j < buckets[i].size(); j++) nums.push_back(buckets[i][j]);
}
}