-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlab2_05_source.cpp
More file actions
228 lines (153 loc) · 4.09 KB
/
lab2_05_source.cpp
File metadata and controls
228 lines (153 loc) · 4.09 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include<iostream>
#include<ctime>
using namespace std;
void merge(int* array, int p, int q, int r);
void merge_sort(int* array, int l, int r);
int quick_partition(int*array, int l, int r);
void quick_sort(int* array, int l, int r);
void counting_sort(int* array, unsigned int size);
void swap(int& a, int& b);
void bubble_sort(int* array, unsigned int size);
void insertion_sort(int* array, unsigned int size);
void selection_sort(int* array, unsigned int size);
int main()
{
srand(time(0));
int size;
int type;
int array[1000];
while(1){
cout <<"Give me the size (max=1000) : ";
cin >> size ;
for (int i=0; i<size; i++)
array[i] = rand() % size ;
for (int i=0; i<size; i++)
cout << array[i] << ' ';
cout << endl;
cout <<"Give me the type of algorithm (0:Bubble, 1:Insert, 2:Selection, 3:Merge, 4:Quick, 5:Counting) : ";
cin >> type;
if (type == 0)
bubble_sort(array, size);
else if (type == 1)
insertion_sort(array, size);
else if (type == 2)
selection_sort(array, size);
else if (type == 3)
merge_sort(array, 0, size-1);
else if (type == 4)
quick_sort(array, 0, size-1);
else if (type == 5)
counting_sort(array, size);
else break;
for (int i=0; i<size; i++)
cout << array[i] << ' ';
}
}
void swap(int& a, int& b){
int temp;
temp = a;
a = b;
b = temp;
}
void merge(int* array, int l, int q, int r){
// initialize new arrays.
int n1 = q - l + 1;
int n2 = r - q;
int* L = new int[n1+1];
int* R = new int[n2+1];
// .....TODO.....
// .....TODO.....
// set the end of the arrays to inifinite
L[n1] = INT_MAX;
R[n2] = INT_MAX;
int i = 0;
int j = 0;
// .....TODO.....
// .....TODO.....
}
void merge_sort(int* array, int l, int r){
// .....TODO.....
// .....TODO.....
}
int quick_partition(int*array, int l, int r){
int pivot = array[r];
int i = l - 1;
// .....TODO.....
// .....TODO.....
return i+1;
}
void quick_sort(int* array, int l, int r){
// .....TODO.....
// .....TODO.....
}
void counting_sort(int* array, unsigned int size){
// Find maximum value
int max_value = array[0];
for (int i = 1; i < size; i ++){
if (array[i] > max_value) {
max_value = array[i];
}
}
// Initialize counting array
int * counts = new int [max_value+1];
for (int i = 0; i < max_value+1; i++){
counts[i] = 0;
}
int* sorted = new int [size];
for(int i = 0; i < size; i++){
sorted[i] = array[i];
}
// .....TODO.....
// .....TODO.....
// update array with sorted array
for (int i = 0; i < size; i++)
array[i] = sorted[i];
}
void bubble_sort(int* array, unsigned int size){
for (unsigned int i=0; i < size-1; i++)
for (unsigned int j=0; j<size-1-i; j++)
{
if (array[j] < array[j+1])
{
int temp;
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
void insertion_sort(int* array, unsigned int size){
for (unsigned int i = 1; i< size; i++)
{
int temp = array[i];
int j = i-1 ;
while(1)
{
if (j<0) break;
if (array[j] <= temp) break;
array[j+1]=array[j];
j--;
}
array[j+1]=temp;
}
}
void selection_sort(int* array, unsigned int size){
for (unsigned int i = 0; i < size-1; i++)
{
int min = array[i];
int min_idx = i;
for (unsigned int j=i+1; j < size; j++ )
{
if (array[j] < min)
{
min = array[j];
min_idx = j;
}
}
// swap(array[i], array[min_idx])
int temp;
temp = array[i];
array[i] = array[min_idx];
array[min_idx] = temp;
}
}