-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortx.cpp
More file actions
325 lines (296 loc) · 5.86 KB
/
Copy pathsortx.cpp
File metadata and controls
325 lines (296 loc) · 5.86 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <string>
#include <GL/glut.h>
#define SORT_NO 4 // Number of sorting algorithms
#define MAX 225 // Number of values in the array
#define SPEED 300 // Speed of sorting, must be greater than MAX always
int a[MAX]; // Array
int swapflag=0; // Flag to check if swapping has occured
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 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]);
}
}
void display_text()
{
glColor3f(0.5,0,1);
bitmap_output(300, 750, "SORT X",GLUT_BITMAP_TIMES_ROMAN_24);
glBegin(GL_LINE_LOOP);
glVertex2f(300, 740);
glVertex2f(360, 740);
glEnd();
if (sorting == 0) // if not sorting display menu
{
bitmap_output(10, 700, "Press s to SORT",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 680, "Press c to SELECT the sort algorithm",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 660, "Press r to RANDOMISE",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 640, "Esc to QUIT",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 620, "Selected sort: ",GLUT_BITMAP_9_BY_15);
bitmap_output(100, 620, *(sort_string+sort_count),GLUT_BITMAP_9_BY_15);
}
else if (sorting == 1) // while sorting
{
glColor3f(0.5,0.5,0);
bitmap_output(10, 640, "Sorting...",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 620, "Press p to PAUSE",GLUT_BITMAP_9_BY_15);
glColor3f(0.0,0.0,0.0);
}
}
void Initialize() {
int temp1;
// Reset the array
for(temp1=0;temp1<MAX;temp1++){
a[temp1]=rand()%500+1;
printf("%d ",a[temp1]);
}
// 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<MAX-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);
display_text();
for(ix = 0; ix < MAX; ix++)
{
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
glVertex2f(10+(800/(MAX+1))*ix,100);
glVertex2f(10+(800/(MAX+1))*(ix+1),100);
glVertex2f(10+(800/(MAX+1))*(ix+1),100+a[ix]*1);
glVertex2f(10+(800/(MAX+1))*ix,100+a[ix]*1);
glEnd();
glColor3f(1,1,1);
}
if (swapflag || sorting == 0)
{
glColor3f(0,0,0);
glBegin(GL_POLYGON);
glVertex2f(10+(800/(MAX+1))*j,100);
glVertex2f(10+(800/(MAX+1))*(j+1),100);
glVertex2f(10+(800/(MAX+1))*(j+1),100+a[j]*1);
glVertex2f(10+(800/(MAX+1))*j,100+a[j]*1);
glEnd();
swapflag=0;
}
glFlush();
}
// Insertion Sort
void insertionsort()
{
int temp;
while(i<MAX)
{
if(flag == 0)
{
j = i;
flag = 1;
}
while(j < MAX - 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 == MAX-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 < MAX - 1)
{
min = a[i + 1];
pos=i+1;
if(i != MAX - 1)
{
for(j = i + 2;j < MAX; 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<MAX-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==MAX-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<MAX-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==MAX-1) {count++; j=MAX-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/MAX,makedelay,1);
}
// Keyboard Function
void keyboard (unsigned char key, int x, int y)
{
if (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;
}
}
if(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("SORTX");
Initialize();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc(1000,makedelay,1);
glutMainLoop();
return 0;
}