-
Notifications
You must be signed in to change notification settings - Fork 1
/
Final.cpp
151 lines (147 loc) · 2.73 KB
/
Final.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
#include <iostream.h>
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include <conio.h>
#define MAX 10
#define ValueOf(x) (x.value())
#define Exchange(x,y) (x.exchange(y))
class element
{
int _value;
int _color;
public:
element()
{
_color = 15; //white
}
void get()
{
scanf("%d",&_value);
}
int value()
{
return _value;
}
void exchange(element &e)
{
element temp;
temp = e;
e = *this;
*this = temp;
}
void setcolor(int col)
{
_color = col;
}
void show()
{
textbackground(_color);
if(_color == 15)
{
textcolor(0);
}
else
{
textcolor(15);
}
cprintf(" %2d ",_value);
printf(" ");
}
};
void Display ( element *A , int p , int r )
{
printf("\n ");
for(int i=p;i<r;i++)
{
A[i].show();
}
}
int partition(element *A ,int p ,int r)
{
int pivot , i = 1 , j = 1;
pivot = ValueOf(A[r]);
A[r].setcolor(RED);
i = p - 1;
for(j=p;j<r;j++)
{
int flag=0;
if( ValueOf(A[j]) <= pivot )
{
i = i + 1;
Exchange(A[j],A[i]);
if(j!=i)
flag=1;
}
else
{
A[j].setcolor(BLUE);
}
Display ( A , 0 , MAX );
if(flag==1)
printf("\ti: %d <swap> j:%d",i,j);
else
printf("\ti: %d\t\tj:%d",i,j);
delay ( 100 );
}
Exchange(A[r],A[i+1]);
A[i+1].setcolor ( GREEN );
printf("\n");
Display ( A , 0 , MAX );
printf ( " > %d at correct position. \n", ValueOf ( A[i+1] ) );
getch();
return i+1 ;
}
void QuickSort(element arr[], int low, int high)
{
int q;
if (low < high)
{
/*q is partitioning index*/
q = partition(arr, low, high);
QuickSort(arr, low, q - 1);
QuickSort(arr, q + 1, high);
}
}
int main()
{
int x=120,i,gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi");
settextstyle(9,HORIZ_DIR,3);
/*int co[] = {100, 50, 900, 50, 900, 260, 100, 260, 100, 50};
drawpoly(4, co);*/
setcolor(GREEN);
outtextxy(x,75,"LOADING ALGO PROJECT");
setcolor(YELLOW);
outtextxy(x,135," QUICK SORT");
setcolor(WHITE);
outtextxy(x,230," PLEASE WAIT");
setcolor(CYAN);
for(i=0;i<400;++i)
{
delay(20);
line(x,200,x,220);
x++;
}
clrscr;
closegraph();
element array[MAX];
i=1;
textbackground(0);
textcolor(15);
clrscr();
printf("\nEnter %d elements:- > ",MAX);
for(i=0;i<MAX;i++)
array[i].get();
Display ( array , 0 , MAX );
printf ( " > Given Array.");
getch();
printf("\n ");
QuickSort(array,0,MAX-1);
printf("\n Array Sorted:\n ");
for(i=0;i<MAX;i++)
array[i].show();
printf("\n");
getch();
return 0;
}