-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.cpp
More file actions
361 lines (326 loc) · 7.39 KB
/
sort.cpp
File metadata and controls
361 lines (326 loc) · 7.39 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<GL\glut.h>
#define SORT_NO 4
#define TOT 20
//#define MAX 50 // Number of values in the array
//#define SPEED 700 // Speed of sorting, must be greater than MAX always
int a[TOT];
int SPEED = 700; // Array
int swapflag=0; // Flag to check if swapping has occurred
int i=0,j=0; // To iterate through the array
int flag=0; // For Insertion Sort
int dirflag=0; // For Ripple Sort, to change direction at the ends
int count=1; // For Ripple Sort, to keep count of how many are sorted at the end
int k=0; // To Switch from Welcome screen to Main Screen
int sorting=0; // 1 if Sorted
char *sort_string[]={"Bubble Sort","Selection Sort","Insertion Sort","Ripple Sort"};
int sort_count=0; // To cycle through the string
// Function to display text on screen char by char
void bitmap_output(int x, int y, char *string, void *font)
{
int len, i;
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}
// Function to convert integer to string
void int_str(int rad,char r[])
{
itoa(rad,r,10); //convert integer to null-terminated string
}
void display_text() //second page
{
glColor3f(0,0,0);
bitmap_output(150, 665, "DEMONSTRATION OF SORTING ALGORITHMS",GLUT_BITMAP_TIMES_ROMAN_24);
glBegin(GL_LINE_LOOP); //underline
glVertex2f(147, 660);
glVertex2f(520, 660);
glEnd();
// other text small font
bitmap_output(10, 625, "This program sorts a random set of numbers in ascending order displaying them graphically as ",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 605, "bars with varying height",GLUT_BITMAP_9_BY_15);
if (sorting == 0) // if not sorting display menu
{
bitmap_output(10, 575, "MENU",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 555, "Press s to SORT",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 535, "Press c to SELECT the sort algorithm",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 515, "Press r to RANDOMISE",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 495, "Esc to QUIT",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 475, "Selected sort: ",GLUT_BITMAP_9_BY_15);
bitmap_output(150, 475, *(sort_string+sort_count),GLUT_BITMAP_9_BY_15); //display the selected sort method
}
else if (sorting == 1) // while sorting
{
glColor3f(0.5,0.5,0.5); //gray
bitmap_output(10, 555, "Sorting in progress...",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 535, "Press p to PAUSE",GLUT_BITMAP_9_BY_15);
glColor3f(0.0,0.0,0.0);
}
}
void front()
{
bitmap_output(150, 475, " DEMONSTRATION OF SORTING ALGORITHMS ",GLUT_BITMAP_TIMES_ROMAN_24);
glBegin(GL_LINE_LOOP); //underline
glVertex2f(145, 470);
glVertex2f(535, 470);
glEnd();
bitmap_output(300, 400, "by Simran Kumaran ",GLUT_BITMAP_HELVETICA_18);
glColor3f(1.0,1.0,1.0);
glBegin(GL_QUADS);
glVertex2f(520,120.0);glVertex2f(520,170);glVertex2f(796,170);glVertex2f(796,120.0);
glEnd();
glColor3f(0.0,0.0,0.0);
bitmap_output(530, 125, "Press Enter to continue!",GLUT_BITMAP_HELVETICA_18);
}
void Initialize() {
int temp1;
// Reset the array
for(temp1=0;temp1<TOT;temp1++){
a[temp1]=rand()%100+1; //generate random numbers in a specified range
printf("%d ",a[temp1]); //displaying the array of random numbers
}
// Reset all values
i=j=0;
dirflag=0;
count=1;
flag=0;
glClearColor(1,1,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 699,0,799);
}
// Return 1 if not sorted
int notsorted(){
int q;
for(q=0;q<TOT-1;q++)
{
if(a[q]>a[q+1])
return 1;
}
return 0;
}
// Main function for display
void display()
{
int ix,temp;
glClear(GL_COLOR_BUFFER_BIT);
if(k==0)
front(); //first page, enter is not pressed yet
else{
display_text(); //second page
char text[10];
for(ix=0;ix<TOT;ix++) //MAX is 50
{
glColor3f(0,0,0); //color of bar
glBegin(GL_LINE_LOOP); //drawing the bar
glVertex2f(10+(700/(TOT+1))*ix,50); //bottom
glVertex2f(10+(700/(TOT+1))*(ix+1),50); //left
glVertex2f(10+(700/(TOT+1))*(ix+1),50+a[ix]*4); //top
glVertex2f(10+(700/(TOT+1))*ix,50+a[ix]*4); //right
glEnd();
int_str(a[ix],text);
//printf("\n%s",text);
glColor3f(0,0,0);
bitmap_output(12+(700/(TOT+1))*ix, 35, text,GLUT_BITMAP_TIMES_ROMAN_10); //display that number below the bar
}
if(swapflag || sorting==0)
{
glColor3f(0,1,0); //cursor
glBegin(GL_POLYGON);
glVertex2f(10+(700/(TOT+1))*j,50);
glVertex2f(10+(700/(TOT+1))*(j+1),50);
glVertex2f(10+(700/(TOT+1))*(j+1),50+a[j]*4);
glVertex2f(10+(700/(TOT+1))*j,50+a[j]*4);
glEnd();
swapflag=0;
}
}
glFlush();
}
// Insertion Sort
void insertionsort()
{
int temp;
while(i<TOT)
{
if(flag==0){j=i; flag=1;}
while(j<TOT-1)
{
if(a[j]>a[j+1])
{
swapflag=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
goto A;
}
j++;
if(j==TOT-1){flag=0;}
printf("swap %d and %d\n",a[j],a[j+1]);
}
i++;
}
sorting=0;
A:
i=j=0;
}
// Selection Sort
void selectionsort()
{
int temp,j,min,pos;
while(notsorted())
{
while(i<TOT-1)
{
min=a[i+1];
pos=i+1;
if(i!=TOT-1)
{
for(j=i+2;j<TOT;j++)
{
if(min>a[j])
{
min=a[j];
pos=j;
}
}
}
printf("\ni=%d min=%d at %d",i,min,pos);
printf("\nchecking %d and %d",min,a[i]);
if(min<a[i])
{
//j=pos;
printf("\nswapping %d and %d",min,a[i]);
temp=a[pos];
a[pos]=a[i];
a[i]=temp;
goto A;
}
i++;
}
}
sorting=0;
i=j=0;
A: printf("");
}
//Bubble Sort
void bubblesort()
{
int temp;
while(notsorted())
{
while(j<TOT-1)
{
if(a[j]>a[j+1])
{
swapflag=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
goto A;
}
j++;
if(j==TOT-1) j=0;
printf("swap %d and %d\n",a[j],a[j+1]);
}
}
sorting=0;
A: printf("");
}
//Ripple Sort
void ripplesort()
{
int temp;
while(notsorted() && sorting)
{
if(dirflag==0)
{
while(j<TOT-1)
{
if(a[j]>a[j+1])
{
swapflag=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
goto A;
}
j++;
if(j==TOT-1) {count++; j=TOT-count; dirflag=1-dirflag;}
printf("j=%d forward swap %d and %d\n",j,a[j],a[j+1]);
}
}
else
{
while(j>=0)
{
if(a[j]>a[j+1])
{
swapflag=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
goto A;
}
j--;
if(j==0){ dirflag=1-dirflag;}
printf("j=%d backward swap %d and %d\n",j,a[j],a[j+1]);
}
}
}
sorting=0;
A: printf("");
}
// Timer Function, takes care of sort selection
void makedelay(int)
{
if(sorting)
{
switch(sort_count)
{
case 0: bubblesort(); break;
case 1: selectionsort(); break;
case 2: insertionsort(); break;
case 3: ripplesort(); break;
}
}
glutPostRedisplay();
glutTimerFunc(SPEED/TOT,makedelay,1);
}
// Keyboard Function
void keyboard (unsigned char key, int x, int y)
{
if(key==13) //enter
k=1;
if (k==1 && sorting!=1)
{
switch (key)
{
case 27 : exit (0); // 27 is the ascii code for the ESC key
case 's' : sorting = 1; break;
case 'r' : Initialize(); break;
case 'c' : sort_count=(sort_count+1)%SORT_NO; break; //choosing the method of sorting
}
}
if(k==1 && sorting==1)
if(key=='p') sorting=0;
}
// Main Function
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1000,600);
glutInitWindowPosition(0,0);
glutCreateWindow("Dynamic Sorting Visualizer");
Initialize();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc(1000,makedelay,1);
glutMainLoop();
}