99 lines
1.6 KiB
C++
99 lines
1.6 KiB
C++
|
#include <iostream>
|
||
|
#include <cstdlib>
|
||
|
#include <vector>
|
||
|
#include <ctime>
|
||
|
using namespace std;
|
||
|
|
||
|
#define N 20
|
||
|
|
||
|
void sort_shell(vector<int>* v){
|
||
|
for(int gap = v->size()/2; gap>0; gap/=2){
|
||
|
for(int i = gap; i < v->size(); i++){
|
||
|
int temp = v->at(i);
|
||
|
|
||
|
int j;
|
||
|
for(j=i; j >= gap && v->at(j-gap) > temp; j-=gap){
|
||
|
v->at(j) = v->at(j-gap);
|
||
|
}
|
||
|
|
||
|
v->at(j) = temp;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void sort_heap_heapify(vector<int>* v, int n, int i){
|
||
|
int smallest = i;
|
||
|
int left = 2 * i + 1;
|
||
|
int right = 2 * i + 2;
|
||
|
|
||
|
if(left < n && v->at(left) < v->at(smallest)){
|
||
|
smallest = left;
|
||
|
}
|
||
|
|
||
|
if(right < n && v->at(right) < v->at(smallest)){
|
||
|
smallest = right;
|
||
|
}
|
||
|
|
||
|
if(smallest != i){
|
||
|
swap(v->at(i), v->at(smallest));
|
||
|
|
||
|
sort_heap_heapify(v, n, smallest);
|
||
|
}
|
||
|
}
|
||
|
void sort_heap(vector<int>* v){
|
||
|
for(int i = v->size()/2 - 1; i>=0; i--){
|
||
|
sort_heap_heapify(v, v->size(), i);
|
||
|
}
|
||
|
|
||
|
for(int i = v->size() - 1; i>0; i--){
|
||
|
swap(v->at(0), v->at(i));
|
||
|
|
||
|
sort_heap_heapify(v, i, 0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main(){
|
||
|
ios::sync_with_stdio(false);
|
||
|
|
||
|
vector<int> v;
|
||
|
|
||
|
srand(time(NULL));
|
||
|
|
||
|
cout<<"The vector:\n";
|
||
|
for(int i=0; i<N; i++){
|
||
|
v.push_back(rand()%5000);
|
||
|
cout<<v.back()<<" ";
|
||
|
}
|
||
|
cout<<"\n";
|
||
|
|
||
|
vector<int> par;
|
||
|
vector<int> impar;
|
||
|
for(int i=0; i<v.size(); i++){
|
||
|
if(i%2){
|
||
|
impar.push_back(v[i]);
|
||
|
}
|
||
|
else{
|
||
|
par.push_back(v[i]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sort_shell(&par);
|
||
|
sort_heap(&impar);
|
||
|
|
||
|
v.clear();
|
||
|
for(int i=0; i<par.size(); i++){
|
||
|
v.push_back(par[i]);
|
||
|
if(i < impar.size()){
|
||
|
v.push_back(impar[i]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cout<<"The ordered vector:\n";
|
||
|
for(int i=0; i<v.size(); i++){
|
||
|
cout<<v[i]<<" ";
|
||
|
}
|
||
|
cout<<endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|