#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
void bubleSort(std::vector<int>& vec)
{
auto size = vec.size();
bool flag = false;
for(decltype(size) i = 0; i < size; i++)
{
flag = false;
for(decltype(size) j = 0; j < size - 1 -i; j++)
{
if(vec[j] > vec[j + 1])
{
flag = true;
std::swap(vec[j], vec[j+1]);
}
}
if(not flag)
{
break;
}
}
}
void selectSort(std::vector<int>& vec)
{
auto size = vec.size();
int minIndex;
for(decltype(size) i = 0; i < size; i++)
{
minIndex = i;
for(decltype(size) j = i + 1; j < size; j++)
{
if(vec[minIndex] > vec[j])
{
minIndex = j;
}
}
std::swap(vec[minIndex], vec[i]);
}
}
void insertSort(std::vector<int>& vec)
{
auto size = vec.size();
for(decltype(size) i = 1; i < size; i++)
{
if(vec[i] < vec[i-1])
{
int j = i - 1;
int tmp = vec[i];
while(j >= 0 and tmp < vec[j])
{
vec[j+1] =vec[j];
j--;
}
vec[j+1] = tmp;
}
}
}
void quickSort(std::vector<int>& vec, int low , int high)
{
if(low >= high)
{
return ;
}
int first = low;
int last = high;
int key = vec[first];
while( first < last)
{
while(first < last and vec[last] > key)
{
last--;
}
if(first < last)
{
vec[first++] = vec[last];
}
while(first < last and vec[first] <= key)
{
first++;
}
if(first < last)
{
vec[last--] = vec[first];
}
}
vec[first] = key;
quickSort(vec, low, first -1);
quickSort(vec, first + 1, high);
}
void shellSortCore(std::vector<int>& vec, int gap, int i)
{
int insertNum = vec[i];
int j;
for(j = i - gap; j >= 0 and insertNum < vec[j]; j-=gap)
{
vec[j+gap] = vec[j];
}
vec[j + gap] = insertNum;
}
void shellSort(std::vector<int>& vec)
{
auto size = vec.size();
for(decltype(size) gap = size/2; gap > 0; gap /=2)
{
for(decltype(size) i = gap; i < size; i++)
{
shellSortCore(vec, gap, i);
}
}
}
void mergeSort(vector<int>& data)
{
int size = data.size();
vector<int> tmp(size, 0);
for(int seg = 1; seg < size; seg += seg)
{
for(int start = 0; start < size; start += seg + seg)
{
int low = start;
int mid = std::min(start + seg, size);
int high = std::min(start + seg + seg, size);
int index = low;
int start1 = low;
int end1 = mid;
int start2 = mid;
int end2 = high;
while(start1 < end1 and start2 < end2)
{
tmp[index++] = data[start1] < data[start2] ? data[start1++] : data[start2++];
}
while(start1 < end1)
{
tmp[index++] = data[start1++];
}
while(start2 < end2)
{
tmp[index++] = data[start2++];
}
}
std::swap(data, tmp);
}
}
void heapify(vector<int>& nums, int n, int i)//对有一定顺序的堆,
//当前第i个结点取根左右的最大值(这个操作称heapfiy)
{
int l = i * 2 + 1;
int r = i * 2 + 2;
int max = i;
if (l<n && nums[l]>nums[max])
{
max = l;
}
if (r<n && nums[r]>nums[max])
{
max = r;
}
if (max != i)
{
swap(nums[max], nums[i]);
heapify(nums, n, max);
}
}
void heapify_build(vector<int>& nums, int n)//建立大根堆,从树的倒数第二层第一个结点开始,
//对每个结点进行heapify操作,然后向上走
{
int temp = (n - 2) / 2;
for (int i = temp; i >= 0; i--)
{
heapify(nums, n, i);
}
for (int i = 0; i < nums.size(); i++)
cout << nums[i] << " ";
cout << endl;
}
void heapify_sort(vector<int>& nums)
//建立大根堆之后,每次交换最后一个结点和根节点(最大值),
//对交换后的根节点继续进行heapify(此时堆的最后一位是最大值,因此不用管他,n变为n-1)
{
int n = nums.size();
heapify_build(nums, n);
for (int i = 0; i < n; i++)
{
swap(nums.front(), nums[n - i - 1]);
heapify(nums, n - i - 1, 0);
}
}
int main()
{
std::vector<int> vec = {9, 7, 8, 6, 1, 3, 2, 4};
//bubleSort(vec);
//selectSort(vec);
//insertSort(vec);
//quickSort(vec, 0, vec.size() - 1);
//shellSort(vec);
//mergeSort(vec);
heapify_sort(vec);
for(const auto& iter : vec)
{
cout << iter << " ";
}
cout << endl;
return 0;
}
各种排序
最新推荐文章于 2022-11-30 11:39:04 发布