-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquickSort.js
More file actions
35 lines (29 loc) · 754 Bytes
/
quickSort.js
File metadata and controls
35 lines (29 loc) · 754 Bytes
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
function quickSort(arr, low, high) {
if (low < high) {
var index = getIndex(arr, low, high)
console.log('index = ' + index)
quickSort(arr, low, index - 1)
quickSort(arr, index + 1, high)
}
}
function getIndex(arr, low, high) {
var tmp = arr[low]
if (low < high) {
while (low < high && arr[high] > tmp) {
high--
}
arr[low] = arr[high]
console.log('high = ' + high)
console.log(arr)
while (low < high && arr[low] < tmp) {
low++
}
arr[high] = arr[low]
console.log('low = ' + low)
console.log(arr)
}
arr[low] = tmp
return low;
}
var arr = [3, 1, 4, 5, 2]
quickSort(arr, 0, arr.length - 1)