Skip to content

Commit

Permalink
Merge pull request #117 from mohamedkhaledyousef/master
Browse files Browse the repository at this point in the history
Add quick-sort.js
  • Loading branch information
ravivarshney01 authored Oct 4, 2018
2 parents f40ca67 + 2d62b9b commit 1cd2075
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Sorting/quickSort/Js/quick-sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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

0 comments on commit 1cd2075

Please sign in to comment.