-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_sort.cpp
237 lines (191 loc) · 4.88 KB
/
main_sort.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
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
#include <iostream>
#include <graphics.h>
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
// Initialize the size with the total numbers to sorted
// and the gap to be maintained in graph
vector<int> numbers;
int size = 200;
int gap = 4;
// Function for swapping the lines graphically
void swap(int i, int j, int x, int y)
{
// Swapping the first line with the correct line
// by making it black again and then draw the pixel
// for white color.
//setlinestyle(1, 0, 3);
setcolor(GREEN); //green
line(i, size, i, size - x);
setcolor(BLACK); // black
line(i, size, i, size - x);
setcolor(YELLOW); //yellow
line(i, size, i, size - y);
//delay(2);
// Swapping the first line with the correct line
// by making it black again and then draw the pixel
// for white color.
setcolor(BLACK); //green
line(j, size, j, size - y);
setcolor(GREEN); // black
line(j, size, j, size - y);
setcolor(GREEN); //green
line(j, size, j, size - x);
}
// Bubble sort function
void bubbleSort()
{
int temp, i, j;
for (i = 1; i < size; i++)
{
for (j = 0; j < size - i; j++)
{
if (numbers[j] > numbers[j + 1])
{
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
// As we swapped the last two numbers
// just swap the lines with the values.
// This is function call
// for swapping the lines
swap(gap * j + 1, gap * (j + 1) + 1, numbers[j + 1], numbers[j]);
}
}
}
}
// Selection sort function
void selectionSort()
{
int min, i, j, temp;
for (i = 0; i < size; i++)
{
min = i;
for (j = i + 1; j < size; j++)
{
if (numbers[j] < numbers[min])
{
temp = numbers[min];
numbers[min] = numbers[j];
numbers[j] = temp;
swap(gap * min + 1, gap * j + 1, numbers[j], numbers[min]);
}
}
}
}
// Insertion sort function
void insertionSort()
{
int i, j, temp;
for (i = 1; i < size; i++)
{
temp = numbers[i];
j = i - 1;
// Move elements of arr[0..i-1],
// that are greater than key, to one
// position ahead of their
// current position
while (j >= 0 && numbers[j] > temp)
{
numbers[j + 1] = numbers[j];
swap(gap * j + 1, gap * (temp) + 1, numbers[temp], numbers[j]);
j = j - 1;
}
numbers[j + 1] = temp;
}
}
// Function to shuffle the array
void disrupt()
{
random_shuffle(numbers.begin(), numbers.end());
cleardevice();
setcolor(YELLOW);
for (int i = 1; i <= gap * size; i += gap)
{
line(i, size, i, (size - numbers[i / gap]));
//rectangle(i, (size - numbers[i / gap]), (i+gap), size);
}
}
// Driver program
int main()
{
int ch=-1, ans;
// auto detection of screen size
int gd = DETECT, gm;
int wid1;
//initgraph(&gd, &gm, NULL);
// Graph initialization
// setting up window size (gap*size) * (size)
wid1 = initwindow(gap * size + 1, size + 1);
setcurrentwindow(wid1);
// Initializing the array
for (int i = size; i>=0 ; i--)
numbers.push_back(i);
/* int temp;
for(int i=size;i>=0;i--)
{
temp=rand()%100;
numbers.push_back(temp);
}*/
// Find a seed and shuffle the array
// to make it random.
// Here different type of array
// can be taken to results
// such as nearly sorted, already sorted,
// reverse sorted to visualize the result
//unsigned seed = chrono::system_clock::now().time_since_epoch().count();
random_shuffle(numbers.begin(), numbers.end());
// Initial plot of numbers in graph taking
// the vector position as x-axis and its
// corresponding value will be the height of line.
for (int i = 1; i <= gap * size; i += gap)
{
line(i, size, i, (size - numbers[i / gap]));
//rectangle(i, (size - numbers[i / gap]), (i+gap), size);
}
// Delay the code
//delay(200);
while(ch!=NULL)
{
system("cls");
cout << "\n\n***** MENU *****\n\n1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n4. Shuffle\n5. Exit\n\nEnter your choice: ";
cin >> ch;
switch(ch)
{
case 1:
bubbleSort();
break;
case 2:
selectionSort();
break;
case 3:
insertionSort();
break;
case 4:
disrupt();
break;
case 5:
cout<<"\nDATA USED:\n";
for (int i = 0; i < size; i++)
{
cout << numbers[i] << " ";
}
cout << "\n\nProcess exited after great time with return value \"THANK YOU\"\n\n\n\n\n";
ans=-1;
break;
// Close the graph
closegraph();
}
if(ans == -1)
{
break;
}
/*cout << "\nDo you want to continue? (y/n) ";
cin >> ans;*/
}
return 0;
}