#include <list>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <climits>
using namespace std;
#include <math.h>
void quick_sort(vector<int> &vec, int left, int right)
{
if (left >= right)
return;
int mid = left + (right - left) / 2;
int i = left - 1;
int j = right + 1;
int target = vec[mid];
while (i < j)
{
do
i++;
while (vec[i] < target);
do
j--;
while (vec[j] > target);
if (i < j)
swap(vec[i], vec[j]);
}
quick_sort(vec, left, j);
quick_sort(vec, j + 1, right);
return;
}
void mearge_sort(vector<int> &vec, vector<int> &temp, int left, int right)
{
if (left >= right)
return;
int mid = left + (right - left) / 2;
mearge_sort(vec, temp, left, mid);
mearge_sort(vec, temp, mid + 1, right);
int i = left;
int j = mid + 1;
int k = left;
while (i <= mid && j <= right)
{
if (vec[i] < vec[j])
{
temp[k] = vec[i];
k++;
i++;
}
else
{
temp[k] = vec[j];
k++;
j++;
}
}
while (i <= mid)
{
temp[k] = vec[i];
k++;
i++;
}
while (j <= right)
{
temp[k] = vec[j];
k++;
j++;
}
for (int i = left, j = left; i <= right; i++, j++)
{
vec[i] = temp[j];
}
return;
}
class Heap
{
private:
int heap_size_;
public:
vector<int> vec;
public:
Heap(vector<int> &nums) : vec(nums.begin(), nums.end())
{
heap_size_ = vec.size();
}
~Heap() = default;
void sift_up(int index)
{
int rem = vec[index];
while (index > 0)
{
int p_index = (index - 1) >> 1;
int p_val = vec[p_index];
if (p_val >= rem)
break;
vec[index] = p_val;
index = p_index;
}
vec[index] = rem;
return;
}
void siftdown(int index)
{
int rem = vec[index];
int half = heap_size_ >> 1;
while (index < half)
{
int child_index = 2 * index + 1;
int child_val = vec[child_index];
if (child_index + 1 < heap_size_)
{
if (child_val < vec[child_index + 1])
{
child_index = child_index + 1;
child_val = vec[child_index];
}
}
if (child_val <= rem)
break;
vec[index] = child_val;
index = child_index;
}
vec[index] = rem;
return;
}
void makeheap1()
{
for (int i = 1; i < heap_size_; i++)
{
sift_up(i);
}
}
void makeheap2()
{
for (int i = heap_size_ >> 1 - 1; i >= 0; i--)
{
siftdown(i);
}
}
void sortheap()
{
while (heap_size_ > 0)
{
int rem = vec[0];
swap(vec[0], vec[heap_size_ - 1]);
heap_size_--;
siftdown(0);
}
}
};
void bubblesort(vector<int> &vec)
{
for (int end = vec.size() - 1; end > 0; end--)
{
int last_swap = 0;
for (int start = 1; start <= end; start++)
{
if (vec[start] < vec[start - 1])
{
swap(vec[start], vec[start - 1]);
last_swap = start;
}
}
end = last_swap;
}
return;
}
void selectsort(vector<int> vec)
{
int max_index = 0;
for (int end = vec.size() - 1; end > 0; end--)
{
for (int start = 1; start <= end; start++)
{
if (vec[start] > vec[max_index])
{
max_index = start;
}
}
swap(vec[max_index], vec[end]);
}
return;
}
class Shellsort
{
public:
vector<int> vec;
Shellsort(vector<int> nums) : vec(nums.begin(), nums.end())
{
}
~Shellsort() = default;
vector<int> GetShellStep(int vec_length)
{
vector<int> step;
int k = 0;
int step_len;
while (true)
{
if (k % 2 == 0)
{
int pow1 = pow(2, k >> 1);
step_len = 9 * (pow1 * pow1 - pow1) + 1;
}
else
{
int pow2 = pow(2,(k - 1) >> 1);
int pow3 = pow(2,(k + 1) >> 1);
step_len = 8*(pow2 *pow3) - 6 * pow3 + 1;
}
if (step_len >=vec_length) break;
step.insert(step.begin(),step_len);
k++;
}
return step;
}
void sort()
{
vector<int> step = GetShellStep(this->vec.size());
for (auto step_len : step) {
for (int col = 0; col < step_len;col ++) {
for (int down_col = col + step_len;down_col < vec.size();down_col +=step_len)
{
int cur = down_col;
while (vec[cur -step_len] > vec[cur]){
swap(vec[cur-step_len],vec[cur]);
cur -=step_len;
}
}
}
}
return ;
}
};
void countingsort(vector<int> &vec)
{
int m_max =INT_MIN;
int m_min =INT_MAX;
for (auto x : vec) {
m_min =min(x,m_min);
m_max =max(m_max,x);
}
int bucket_size = m_max -m_min + 1;
vector<int> bucket(bucket_size,0);
for(auto x :vec) {
bucket[x - m_min] ++;
}
for(int i = 1 ; i <bucket_size; i ++) {
bucket[i] +=bucket[i-1];
}
vector<int> res (vec.size(),0);
for (int i = vec.size() - 1 ;i >= 0; i --) {
int index = bucket[vec[i] - m_min] - 1;
bucket[vec[i] - m_min] -= 1;
res[index] = vec[i];
}
for (auto x : res) {
cout << x << " ";
}
cout << endl;
return ;
}
void radissort(vector<int> & vec)
{
vector<list<int>*>bucket(10,nullptr);
int m_max= INT_MIN;
for (auto x : vec) {
m_max =max(m_max,x);
}
vector<int> res (vec.begin(),vec.end());
int k = 1;
for (;k <= m_max; k =k*10) {
for (auto x : res) {
int index = x / k %10;
if (bucket[index] == nullptr)
bucket[index] = new list<int>();
bucket[index]->push_back(x);
}
int insert_index = 0;
for (auto y : bucket) {
if (y == nullptr)continue;
while (!y->empty()) {
int rem = y->front();
y->pop_front();
res[insert_index] = rem;
insert_index ++;
}
}
}
cout<< endl<<endl<<endl;
for (auto x : res)
cout << x << " ";
return ;
}
int main()
{
vector<int> vec = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5, -6, 11, 21, 2123, 43251, 4352, 456, 65, 23, 67, 3456, -542};
vector<int> temp(vec.size(), 0);
for (auto x : vec)
cout << x << " ";
cout << endl;
countingsort(vec);
radissort(vec);
return 0;
}
|