排序算法
sort.h
#pragma once
#include <iostream>
#include <vector>
using namespace std;
void BubbleSortPositive(std::vector<int> &nums);
void insertSortPositive(vector<int> &array);
void SelectionSort(vector<int> &nums);
void quickSort(vector<int> &nums, int left, int right);
void merge(vector<int> &nums, int left, int mid, int right);
void mergeSort(vector<int> &nums, int left, int right);
void heapSort(vector<int> &nums);
void heapify(vector<int> &nums, int n, int index);
void bucketSort(vector<int> &nums);
void bucketSortEX(vector<int> &nums);
void countSort(vector<int> &nums);
void shellSort(vector<int> &nums);
void radixSort(vector<int> &nums);
sort.cpp
#include "Sort.h"
#include <algorithm>
void BubbleSortPositive(std::vector<int> &nums)
{
int cycle_index = nums.size();
bool is_swap = false;
for (int i = 1; i <= cycle_index; i++)
{
for (int j = 1; j <= cycle_index - i; j++)
{
if (nums[j] < nums[j - 1])
{
swap(nums[j], nums[j - 1]);
is_swap = true;
}
bool check = true;
}
if (!is_swap)
break;
}
}
void insertSortPositive(vector<int> &array)
{
int size = array.size();
for (int i = 1; i < size; i++)
{
for (int j = i; j > 0 && array[j] < array[j - 1]; j--)
{
swap(array[j], array[j - 1]);
}
}
}
void SelectionSort(vector<int> &nums)
{
int size = nums.size();
for (int i = 0; i < size; i++)
{
int min = i;
for (int j = i + 1; j < size; j++)
{
if (nums[j] < nums[min])
{
min = j;
}
}
swap(nums[i], nums[min]);
}
}
void quickSort(vector<int> &nums, int left, int right)
{
if (left + 1 >= right)
return;
int piovt = nums[left];
int low = left, higt = right;
while (low < higt)
{
while (low < higt && nums[higt] >= piovt)
{
higt--;
}
nums[low] = nums[higt];
while (low < higt && nums[low] <= piovt)
{
low++;
}
nums[higt] = nums[low];
}
nums[low] = piovt;
quickSort(nums, left, low);
quickSort(nums, ++low, right);
}
void merge(vector<int> &nums, int left, int mid, int right)
{
// 取一个临时变量存储数据,为了使用方便,大小和原对象大小保持一致
vector<int> temp(right + 1);
// 取左右两份数据的两个索引,第一份数据从left到mid,第二份数据从mid+1到right
int l = left;
int r = mid + 1;
// 取一个temp的索引从left开始,为了存数据
int t = left;
// 当左右的两个索引都没达到终点时,判断大小,小的就放入temp中
while (l <= mid && r <= right)
{
if (nums[l] < nums[r])
{
temp[t++] = nums[l++];
}
else
{
temp[t++] = nums[r++];
}
}
// 当左右的两个索引至少一个达到终点时,则把剩下一个容器的数据接着temp当前顺序存入数据
while (l <= mid)
{
temp[t] = nums[l];
t++;
l++;
}
while (r <= right)
{
temp[t] = nums[r];
t++;
r++;
}
// 再把temp的从left到right到数据给到原对象
for (int i = left; i <= right; i++)
{
nums[i] = temp[i];
}
};
void mergeSort(vector<int> &nums, int left, int right)
{
if (left >= right)
return;
int mid = (left + right) / 2;
// 不断分为左右两份数据
mergeSort(nums, left, mid);
mergeSort(nums, mid + 1, right);
// 分完后按从小到大的顺序合并两份数据,由于是递归,所以分到最小份的时候开始第一次合并,而最后一次合并则是第一次分离的数据
merge(nums, left, mid, right);
};
void heapSort(vector<int> &nums)
{
int size = nums.size();
// 建堆
for (int i = size / 2 - 1; i >= 0; i--)
{
heapify(nums, size, i);
}
// 排序
for (int i = size - 1; i >= 0; i--)
{
swap(nums[i], nums[0]);
heapify(nums, i, 0);
}
};
void heapify(vector<int> &nums, int n, int index)
{
int left = 2 * index + 1;
int right = 2 * index + 2;
int top = index;
if (left < n && nums[left] > nums[top])
{
top = left;
}
if (right < n && nums[right] > nums[top])
{
top = right;
}
if (top != index)
{
swap(nums[index], nums[top]);
heapify(nums, n, top);
}
};
void bucketSort(vector<int> &nums)
{
if (nums.empty())
return;
int low = *min_element(nums.begin(), nums.end());
int high = *max_element(nums.begin(), nums.end());
int n = high - low + 1;
vector<int> bucket(n, 0);
vector<int> res;
for (auto &&x : nums)
{
++bucket[x - low];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < bucket[i]; j++)
{
res.push_back(i + low);
}
}
nums = move(res);
};
void bucketSortEX(vector<int> &nums)
{
int size = nums.size();
int max = *max_element(nums.begin(), nums.end());
int min = *min_element(nums.begin(), nums.end());
int range = max - min;
int bucketNum = (max - min) / range + 1;
vector<vector<int>> bucketArray(bucketNum);
for (auto &&value : nums)
{
int num = (value - min) / range;
bucketArray[num].push_back(value);
}
for (int i = 0; i < bucketNum; i++)
{
quickSort(bucketArray[i], 0, bucketArray[i].size() - 1);
}
for (int i = bucketNum - 1; i > 0; i--)
{
bucketArray[i - 1].insert(bucketArray[i - 1].end(), bucketArray[i].begin(), bucketArray[i].end());
}
nums = move(bucketArray[0]);
};
void countSort(vector<int> &nums)
{
/*
1.判断原数据容量是否大于1
2.获取原容器最大值
3.根据原容器最大值,创建一个该大小的计数数组
4.计数数组的数据,以索引为容器的数据值,以计数数组的数据也就是计数的数量值
5.再把计数数组的数据从第二位数据开始,做累计(当前值=当前值+上一位的值)
6.从以原数据容器的数据根据累计后的计数数组去查找需摆放的位置(计数数组每被取值一次则减一),存入与原数组同大小的数组中
*/
if (nums.size() < 2)
return;
int max = *max_element(nums.begin(), nums.end());
int min = *min_element(nums.begin(), nums.end());
// 计数
vector<int> countBox(max - min + 1, 0);
for (auto &&i : nums)
{
countBox[i - min]++;
}
// 累计
for (int i = 1; i < countBox.size(); i++)
{
countBox[i] += countBox[i - 1];
}
vector<int> temp(nums.size());
for (size_t i = 0; i < nums.size(); i++)
{
temp[countBox[nums[i] - min] - 1] = nums[i];
countBox[nums[i] - min]--;
}
nums = move(temp);
};
void shellSort(vector<int> &nums)
{
int size = nums.size();
for (int half = size / 2; half > 0; half /= 2)
{
for (int right = half; right < size; right++)
{
for (int left = right; left >= half; left -= half)
{
int compare = left - half;
if (nums[left] < nums[compare])
{
swap(nums[left], nums[compare]);
}
}
}
}
};
void radixSort(vector<int> &nums)
{
const int radix = 10;
int max = *max_element(nums.begin(), nums.end());
int size = nums.size();
int base = 1;
while (max / base > 0)
{
vector<int> buckets(10);
for (int i = 0; i < size; i++)
{
buckets[nums[i] / base % 10]++;
}
for (int i = 1; i < 10; i++)
{
buckets[i] += buckets[i - 1];
}
vector<int> temp(size, 0);
for (int i = size - 1; i >= 0; i--)
{
temp[buckets[nums[i] / base % 10] - 1] = nums[i];
buckets[nums[i] / base % 10]--;
}
nums = move(temp);
base *= 10;
}
}