-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
247 lines (200 loc) · 6.03 KB
/
Copy pathmain.cpp
File metadata and controls
247 lines (200 loc) · 6.03 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
///BUBBLE SORT
void bubbleSort( vector<int> numbers) {
for(int i=0; i<numbers.size(); i++) {
for(int j=0; j<(numbers.size()-i-1); j++) {
//checking if the number to the left is larger
if(numbers[j]>numbers[j+1]) {
for (int k=0; k<numbers.size();k++) {
cout <<numbers[k] << " ";
}
cout<<"-->";
// if the number is larger than swap them out.
int temp = numbers[j]; //use a temporary variable
numbers[j]= numbers[j+1]; //swap them
numbers[j+1]=temp; // set the number on the left to the next number
for (int k=0; k<numbers.size();k++) {
cout << numbers[k] << " ";
}
cout<<endl;
}
}
}
}
///SELECTION SORT
void selectionSort(vector<int> numbers) {
int minVal,temp;
for (int i=0; i<numbers.size()-1; i++) {
minVal=i;
for (int j=i+1; j<numbers.size(); j++) {
for (int k=0; k<numbers.size();k++) {
cout <<numbers[k] << " ";
}
cout<<" Min val: " <<numbers[minVal]<<" --> ";
if ( numbers[j]< numbers[minVal]) {
minVal=j;
}
}
temp = numbers[minVal];
numbers[minVal]=numbers[i];
numbers[i]=temp;
for (int k=0; k<numbers.size();k++) {
cout <<numbers[k] << " ";
}
cout<<endl;
}
}
///INSERTION SORT
void insertionSort(vector<int> numbers) {
for(int i=0; i<numbers.size(); i++) {
int value = numbers[i];
int hole = i;
for (int k=0; k<numbers.size();k++) {
cout <<numbers[k] << " ";
}
cout<<" hole at "<<numbers[hole]<<" --->";
while (hole>0 && numbers[hole-1] > value) {
numbers[hole]= numbers[hole-1];
hole--;
}
numbers[hole]=value;
for (int k=0; k<numbers.size();k++) {
cout <<numbers[k] << " ";
}
cout<<endl;
}
}
///MERGE SORT MERGE FUNCTION
vector<int> merge(vector <int> &numbers, vector<int> &left, vector<int> &right) {
int li=0;
int ri=0;
vector<int> temp;
while( li<left.size() && ri< right.size()) {
if (left[li] < right[ri]){
temp.push_back(left[li]);
for (int k=0; k<temp.size();k++) {
cout <<temp[k] << " ";
}
cout<<endl;
li++;
} else {
temp.push_back(right[ri]);
for (int k=0; k<temp.size();k++) {
cout <<temp[k] << " ";
}
cout<<endl;
ri++;
}
}
while(li < left.size()){
// push the rest of the values back because they're sorted
temp.push_back(left[li]);
for (int k=0; k<temp.size();k++) {
cout <<temp[k] << " ";
}
cout<<endl;
li++;
}
while(ri < right.size()){
// do the same for the right
temp.push_back(right[ri]);
for (int k=0; k<temp.size();k++) {
cout <<temp[k] << " ";
}
cout<<endl;
ri++;
}
return temp;
}
///MERGE SORT
vector<int> mergeSort(vector<int> &temp) {
// base case, we are done!
if(temp.size() == 1) {
return temp;
}
// find the middle split
vector<int>::iterator mid = temp.begin() + (temp.size() / 2);
//split into new vectors right and left
vector<int> left(temp.begin(), mid);
vector<int> right(mid, temp.end());
//recursively call on the new right and left vectors
left = mergeSort(left);
// cout<<"\n Left side: ";
// for (int k=0; k<left.size();k++) {
// cout <<left[k] << " ";
// }
// cout<<endl;
right = mergeSort(right);
// cout<<"\n right side: ";
// for (int k=0; k<right.size();k++) {
// cout <<right[k] << " ";
// }
// cout<<endl;
//merge them back
return merge(temp, left, right);
}
///QUICK SORT
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// makes last element pivot, puts the pivot element at its correct position in sorted vector, left smaller right bigger than pivot
int partition (vector<int> &numbers, int low, int high)
{
int pivot = numbers[high]; // pivot number
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++) {
// If current element is smaller than or equal to pivot
if (numbers[j] <= pivot) {
i++; // increment index of smaller element
swap(&numbers[i], &numbers[j]);
}
}
swap(&numbers[i + 1], &numbers[high]);
for (int k=0; k<numbers.size(); k++) {
cout<<numbers[k]<< " ";
}
cout<<endl;
return (i + 1);
}
///QUICK SORT
void quickSort(vector<int> &numbers, int low, int high) {
//checking that there's more than one number in the vector
if (low <= high) {
// pi is partitioning index, vector[p] is now at right place
int pi = partition(numbers, low, high);
// Separately sort elements before and after partition
quickSort(numbers, low, pi - 1);
quickSort(numbers, pi + 1, high);
}
}
int main() {
fstream inFile;
string line;
inFile.open("/Users/eeshaulhaq/CLionProjects/BubbleSort/data.txt");
vector<int> numbers;
//READ THE NUMBERS FROM THE FILE INTO THE NUMBERS VECTOR
if (inFile.is_open()) {
while ( getline (inFile,line) ) {
int num = stoi(line);
numbers.push_back(num);
}
inFile.close();
}
for (int k=0; k<numbers.size(); k++) {
cout<<numbers[k]<< " ";
}
cout<<endl;
quickSort(numbers, 0, numbers.size()-1);
// vector<int> newNumbers = mergeSort(numbers);
// for (int k=0; k<newNumbers.size();k++) {
// cout <<newNumbers[k] << " ";
// }
return 0;
}