forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.js
More file actions
34 lines (28 loc) · 699 Bytes
/
quick_sort.js
File metadata and controls
34 lines (28 loc) · 699 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
function quick_Sort(origArray) {
if (origArray.length<=1)
{
return origArray;
}
else
{
var left=[],right=[],newArray=[];
var pivot=origArray.pop();
var length=origArray.length;
for (var i=0; i<length ;++i)
{
if (origArray[i]<=pivot)
{
left.push(origArray[i]);
}
else
{
right.push(origArray[i]);
}
}
return newArray.concat(quick_Sort(left), pivot, quick_Sort(right));
}
}
var myArray = [3,3,-1, 0, 2, 5, -1, 4, 1 ];
console.log("Original array: " + myArray); //3,3,-1,0,2,5,-1,4,1
var sortedArray = quick_Sort(myArray);
console.log("Sorted array: " + sortedArray); //-1,-1,0,1,2,3,3,4,5