-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick.js
128 lines (106 loc) · 3.5 KB
/
quick.js
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
/*
*****************************
DONE BY : NAVDEEP KHEDE
E-MAIL : [email protected]
*****************************
*/
function Quick()
{
c_delay=0;
quickSort(0,ar_size-1);
enable_buttons();
}
function quickPartition(s, e)
{
var i = s + 1;
var piv = div_sizes[s] ;//make the first element as pivot element.
div_update(divs[s],div_sizes[s],"yellow");//Color update
for(var j =s + 1; j <= e ; j++ )
{
//re-arrange the array by putting elements which are less than pivot on one side and which are greater that on other.
if (div_sizes[ j ] < piv)
{
div_update(divs[j],div_sizes[j],"yellow");//Color update
div_update(divs[i],div_sizes[i],"red");//Color update
div_update(divs[j],div_sizes[j],"red");//Color update
var temp=div_sizes[i];
div_sizes[i]=div_sizes[j];
div_sizes[j]=temp;
div_update(divs[i],div_sizes[i],"red");//Height update
div_update(divs[j],div_sizes[j],"red");//Height update
div_update(divs[i],div_sizes[i],"blue");//Height update
div_update(divs[j],div_sizes[j],"blue");//Height update
i += 1;
}
}
div_update(divs[s],div_sizes[s],"red");//Color update
div_update(divs[i-1],div_sizes[i-1],"red");//Color update
var temp=div_sizes[s];//put the pivot element in its proper place.
div_sizes[s]=div_sizes[i-1];
div_sizes[i-1]=temp;
div_update(divs[s],div_sizes[s],"red");//Height update
div_update(divs[i-1],div_sizes[i-1],"red");//Height update
for(var t=s;t<=i;t++)
{
div_update(divs[t],div_sizes[t],"green");//Color update
}
return i-1;
//return the position of the pivot
// var pivot = div_sizes[s];
// div_update(divs[s],div_sizes[s],"yellow");
// while(s<=e)
// {
// while(div_sizes[s]<pivot)
// {
// div_update(divs[s], div_sizes[s], "red");
// s++;
// div_update(divs[s], div_sizes[s], "blue");
// }
// div_update(divs[s], div_sizes[s], "red");
// while(div_sizes[e]>pivot)
// {
// div_update(divs[e], div_sizes[e], "red");
// e--;
// div_update(divs[e], div_sizes[e], "blue");
// }
// div_update(divs[e], div_sizes[e], "red");
// if(s<=e)
// {
// var temp = div_sizes[s];
// div_sizes[s] = div_sizes[e];
// div_sizes[e] = temp;
// div_update(divs[s], div_sizes[s], "red");
// div_update(divs[e], div_sizes[e], "red");
// s++;
// e--;
// }
// div_update(divs[s],div_sizes[s],"yellow");
// }
// for(var t=0; t<=s; t++)
// {
// div_update(divs[t], div_sizes[t], "green");
// }
// return s;
}
function quickSort(s, e)
{
if( s < e )
{
//stores the position of pivot element
var piv_pos = quickPartition (s, e ) ;
quickSort (s, piv_pos -1);//sorts the left side of pivot.
quickSort (piv_pos +1, e) ;//sorts the right side of pivot.
}
// if(ar_size>1)
// {
// var pivPos = quickPartition(s, e);
// if(s<pivPos-1) quickSort(s, pivPos-1);
// if(pivPos<e) quickSort(pivPos, e);
// }
}
/*
*****************************
DONE BY : NAVDEEP KHEDE
E-MAIL : [email protected]
*****************************
*/