-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick-sort.js
More file actions
41 lines (38 loc) · 899 Bytes
/
quick-sort.js
File metadata and controls
41 lines (38 loc) · 899 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
36
37
38
39
40
41
"use strict";
module.exports = function (){
this.level = 0;
this.queue = [];
this.sort = function(items){
if (!items.length)
return [];
const pivot = items[0];
let left = [];
let right = [];
for(let i = 1; i < items.length; i++){
var item = items[i];
if (item > pivot)
right.push(item);
if (item < pivot)
left.push(item);
}
return this.sort(left).concat(pivot, this.sort(right));
};
this.merge = function(left, right){
var totalItemCount = left.length + right.length;
var result = [];
let j = 0;
let k = 0;
for(let i = 0; i < totalItemCount; i++){
console.log(i + " " + left[j] + " || " + right[k]);
if (!right[k] || left[j] < right[k]){
result.push(left[j]);
j++;
}
else{
result.push(right[k]);
k++;
}
}
return result;
};
};