Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 059848e

Browse files
authored
Merge pull request #25 from Panzki/quickSort
Added quicksort to the sorting algorithms.
2 parents fddccc0 + 781913b commit 059848e

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)

AltStyle によって変換されたページ (->オリジナル) /