-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.js
48 lines (48 loc) · 1.14 KB
/
quickSort.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
function ArrayList(){
var array=[];
this.insert=function(item){
array.push(item);
};
this.toString=function(){
return array.join();
};
var swap=function(array,index1,index2){
var aux=array[index1];
array[index1]=array[index2];
array[index2]=aux;
};
var partition=function(array,left,right){
var pivot=array[Math.floor((right+left)/2)],
i=left,
j=right;
while(i<=j){
while(array[i]<pivot){
i++;
}
while(array[j]>pivot){
j--;
}
if(i<=j){
swap(array,i,j);
i++;
j--;
}
}
return i;
};
var quick=function(array,left,right){
var index;
if(array.length>1){
index=partition(array,left,right);
if(left<index-1){
quick(array,left,index-1);
}
if(index<right){
quick(array,index,right);
}
}
};
this.quickSort=function(){
quick(array,0,array.length-1);
};
}