Skip to content

Commit 781913b

Browse files
committed
Added quicksort to the sorting algorithms.
1 parent cedbe13 commit 781913b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Sorts/quickSort.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy.
3+
* For more information see here: https://en.wikipedia.org/wiki/Quicksort
4+
*/
5+
function quickSort(items) {
6+
7+
var length = items.length;
8+
9+
if (length <= 1) {
10+
return items;
11+
}
12+
var PIVOT = items[0];
13+
var GREATER = [];
14+
var LESSER = [];
15+
16+
for (var i = 1; i < length; i++) {
17+
if (items[i] > PIVOT) {
18+
GREATER.push(items[i]);
19+
} else {
20+
LESSER.push(items[i]);
21+
}
22+
}
23+
24+
var sorted = quickSort(LESSER);
25+
sorted.push(PIVOT);
26+
sorted = sorted.concat(quickSort(GREATER));
27+
28+
return sorted;
29+
}
30+
31+
//Implementation of quick sort
32+
33+
var ar = [0, 5, 3, 2, 2];
34+
//Array before Sort
35+
console.log(ar);
36+
ar = quickSort(ar);
37+
//Array after sort
38+
console.log(ar);

0 commit comments

Comments
 (0)