-
Notifications
You must be signed in to change notification settings - Fork 62
/
main.cpp
171 lines (140 loc) · 3.99 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
::: SourceCode CopyRight @ Yongil Kim
::: Apr.2020 SNU MiLab
*/
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
class arr {
public:
arr();
~arr();
void sort(void (*f)(int*, unsigned int));
void sort(void(*f)(int*, int, int));
void init(int n);
void reset();
void check();
void print();
private:
int* input_array;
int* ref_array;
int size;
};
void swap(int& a, int& b);
void bubble_sort_descending(int* array, unsigned int size);
void insertion_sort_descending(int* array, unsigned int size);
void selection_sort_descending(int* array, unsigned int size);
int main(void) {
srand((unsigned int)time(NULL));
int size;
cout << "Give me the size : ";
cin >> size;
int type_sort;
cout << "Give me the type of algorithm (0: Bubble, 1: Insertion, 2: Selection): ";
cin >> type_sort;
arr mysort;
mysort.init(size);
if (type_sort ==0){
mysort.sort(bubble_sort_descending);
mysort.print();
mysort.reset();
}
else if (type_sort ==1){
mysort.sort(insertion_sort_descending);
mysort.print();
mysort.reset();
}
else if (type_sort ==2){
mysort.sort(selection_sort_descending);
mysort.print();
mysort.reset();
}
return 0;
}
arr::arr() {}
arr::~arr() {
delete[] input_array;
delete[] ref_array;
}
void arr::init(int size) {
this->size = size;
input_array = new int[size];
ref_array = new int[size];
for (int i = 0; i < size; i++) {
int rnd_num;
cin >> rnd_num;
input_array[i] = rnd_num;
ref_array[i] = rnd_num;
}
}
void arr::reset() {
if (ref_array == nullptr) ref_array = new int[size];
for (int i = 0; i < size; i++)
ref_array[i] = input_array[i];
cout << "Reset" << endl << endl;
}
void arr::check() {
bool check = true;
if (ref_array == nullptr)
cout << "Error! The array has no element." << endl;
else {
for (int i = 0; i < size - 1; i++) {
if (ref_array[i] < ref_array[i + 1])
check = false;
}
}
if (check) cout << "Correct!" << endl;
else cout << "Wrong!" << endl;
}
void arr::sort(void(*f)(int*, unsigned int)) {
f(ref_array, size);
}
void arr::sort(void(*f)(int*, int, int)) {
f(ref_array, 0, (int)(size - 1));
}
void arr::print() {
for (int i = 0; i < size; i++){
cout << ref_array[i] ;
cout << " " ;
}
cout << " "<<endl;
}
void swap(int& a, int& b) {
int tmp = a;
a = b;
b = tmp;
}
/*###################################################################################
########################## Incremental Algorithm ###################################
#####################################################################################
*/
void bubble_sort_descending(int* array, unsigned int size) {
for (unsigned int i = 0; i < size - 1; i++)
{
//#######################################################
//Implement here
;
//#######################################################
}
}
void insertion_sort_descending(int* array, unsigned int size) {
for (unsigned int i = 1; i < size; i++)
{
//#######################################################
//Implement here
;
//#######################################################
}
}
void selection_sort_descending(int* array, unsigned int size) {
for (unsigned int i = 0; i < size - 1; i++)
{
//#######################################################
//Implement here
;
//#######################################################
}
}