chore: Lab05
This commit is contained in:
parent
f5645fca66
commit
b379440f66
159
lab05/01.cpp
Normal file
159
lab05/01.cpp
Normal file
@ -0,0 +1,159 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
#define SIZE 5000
|
||||
|
||||
void benchmark(void(*f)(vector<int>* v), vector<int>* v);
|
||||
void sort_shell(vector<int>* v);
|
||||
void sort_heap(vector<int>* v);
|
||||
void sort_quick_driver(vector<int>* v);
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(false);
|
||||
|
||||
cout<<"----- Testing sort algorithms -----\n\n\n";
|
||||
cout<<"Usage: e[X]it, [C]reate vector, print [V]ector, [R]eset vector, [S]hell sort, [H]eap sort, [Q]uick sort\n\n";
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
vector<int> v;
|
||||
vector<int> work;
|
||||
|
||||
while(true){
|
||||
char command;
|
||||
cout<<"> ";
|
||||
cin>>command;
|
||||
|
||||
switch(command){
|
||||
case 'X':
|
||||
case 'x':
|
||||
cout<<"Exiting..."<<endl;
|
||||
return 0;
|
||||
case 'C':
|
||||
case 'c':
|
||||
cout<<"Initializing vector...\n";
|
||||
v.clear();
|
||||
for(int i=0; i<SIZE; i++){
|
||||
v.push_back(rand()%100000);
|
||||
}
|
||||
work = v;
|
||||
cout<<"Done\n";
|
||||
break;
|
||||
case 'V':
|
||||
case 'v':
|
||||
cout<<"The vector:\n";
|
||||
for(vector<int>::iterator it = work.begin(); it < work.end(); it++){
|
||||
cout<<*it<<" ";
|
||||
}
|
||||
cout<<"\n";
|
||||
break;
|
||||
case 'R':
|
||||
case 'r':
|
||||
cout<<"Reinitializing working copy...\n";
|
||||
work = v;
|
||||
cout<<"Done\n";
|
||||
break;
|
||||
case 'S':
|
||||
case 's':
|
||||
cout<<"# Shell sort\n";
|
||||
benchmark(sort_shell, &work);
|
||||
break;
|
||||
case 'H':
|
||||
case 'h':
|
||||
cout<<"# Heap sort\n";
|
||||
benchmark(sort_heap, &work);
|
||||
break;
|
||||
case 'Q':
|
||||
case 'q':
|
||||
cout<<"# Quick sort\n";
|
||||
benchmark(sort_quick_driver, &work);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void benchmark(void(*f)(vector<int>* v), vector<int>* v){
|
||||
auto t1 = chrono::high_resolution_clock::now();
|
||||
(*f)(v);
|
||||
auto t2 = chrono::high_resolution_clock::now();
|
||||
auto duration = chrono::duration_cast<chrono::nanoseconds>(t2 - t1).count();
|
||||
cout<<"Execution time: "<<duration<<" ns\n";
|
||||
}
|
||||
|
||||
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 largest = i;
|
||||
int left = 2 * i + 1;
|
||||
int right = 2 * i + 2;
|
||||
|
||||
if(left < n && v->at(left) > v->at(largest)){
|
||||
largest = left;
|
||||
}
|
||||
|
||||
if(right < n && v->at(right) > v->at(largest)){
|
||||
largest = right;
|
||||
}
|
||||
|
||||
if(largest != i){
|
||||
swap(v->at(i), v->at(largest));
|
||||
|
||||
sort_heap_heapify(v, n, largest);
|
||||
}
|
||||
}
|
||||
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 sort_quick_partition(vector<int>* v, int low, int high){
|
||||
int pivot = v->at(high);
|
||||
int i = low - 1;
|
||||
|
||||
for(int j = low; j <= high - 1; j++){
|
||||
if(v->at(j) < pivot){
|
||||
i++;
|
||||
swap(v->at(i), v->at(j));
|
||||
}
|
||||
}
|
||||
swap(v->at(i+1), v->at(high));
|
||||
|
||||
return i + 1;
|
||||
}
|
||||
void sort_quick(vector<int>* v, int low, int high){
|
||||
if(low < high){
|
||||
int parti = sort_quick_partition(v, low, high);
|
||||
|
||||
sort_quick(v, low, parti - 1);
|
||||
sort_quick(v, parti + 1, high);
|
||||
}
|
||||
}
|
||||
void sort_quick_driver(vector<int>* v){
|
||||
sort_quick(v, 0, v->size() - 1);
|
||||
}
|
98
lab05/02.cpp
Normal file
98
lab05/02.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
#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;
|
||||
}
|
78
lab05/03.cpp
Normal file
78
lab05/03.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class Subscriber{
|
||||
public:
|
||||
string name, address, phone;
|
||||
double invoice;
|
||||
|
||||
void print(){
|
||||
cout<<"- Name: "<<name<<"\n";
|
||||
cout<<" Address: "<<address<<"\n";
|
||||
cout<<" Phone: "<<phone<<"\n";
|
||||
cout<<" Invoice value: "<<invoice<<"\n";
|
||||
}
|
||||
};
|
||||
|
||||
int sort_quick_partition(vector<Subscriber>* v, int low, int high){
|
||||
int pivot = v->at(high).invoice;
|
||||
int i = low - 1;
|
||||
|
||||
for(int j = low; j <= high - 1; j++){
|
||||
if(v->at(j).invoice > pivot){
|
||||
i++;
|
||||
swap(v->at(i), v->at(j));
|
||||
}
|
||||
}
|
||||
swap(v->at(i+1), v->at(high));
|
||||
|
||||
return i + 1;
|
||||
}
|
||||
void sort_quick(vector<Subscriber>* v, int low, int high){
|
||||
if(low < high){
|
||||
int parti = sort_quick_partition(v, low, high);
|
||||
|
||||
sort_quick(v, low, parti - 1);
|
||||
sort_quick(v, parti + 1, high);
|
||||
}
|
||||
}
|
||||
void sort_quick_driver(vector<Subscriber>* v){
|
||||
sort_quick(v, 0, v->size() - 1);
|
||||
}
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(false);
|
||||
|
||||
cout<<"Number of subscribers: ";
|
||||
int n;
|
||||
cin>>n;
|
||||
|
||||
vector<Subscriber> v;
|
||||
for(int i=0; i<n; i++){
|
||||
Subscriber s;
|
||||
cin.ignore(5, '\n');
|
||||
cout<<"- Name: ";
|
||||
getline(cin, s.name);
|
||||
cout<<" Address: ";
|
||||
getline(cin, s.address);
|
||||
cout<<" Phone: ";
|
||||
getline(cin, s.phone);
|
||||
cout<<" Invoice value: ";
|
||||
cin>>s.invoice;
|
||||
|
||||
v.push_back(s);
|
||||
}
|
||||
|
||||
sort_quick_driver(&v);
|
||||
|
||||
cout<<"\n\n\nTop 3 invoice values:\n";
|
||||
for(int i=0; i<3; i++){
|
||||
v[i].print();
|
||||
}
|
||||
|
||||
cout.flush();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user