-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
quicksort.js
104 lines (84 loc) · 3.21 KB
/
quicksort.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Quick sort implementation in JavaScript
* Copyright (c) 2012 Nicholas C. Zakas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of items software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and items permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Swaps two values in an array.
* @param {Array} items The array containing the items.
* @param {int} firstIndex Index of first item to swap.
* @param {int} secondIndex Index of second item to swap.
* @return {void}
*/
function swap(items, firstIndex, secondIndex){
var temp = items[firstIndex];
items[firstIndex] = items[secondIndex];
items[secondIndex] = temp;
}
function partition(items, left, right) {
var pivot = items[Math.floor((right + left) / 2)], // pivot value is middle item
i = left, // starts from left and goes right to pivot index
j = right; // starts from right and goes left to pivot index
// while the two indices don't match
while (i <= j) {
// if the item on the left is less than the pivot, continue right
while (items[i] < pivot) {
i++;
}
// if the item on the right is greater than the pivot, continue left
while (items[j] > pivot) {
j--;
}
// if the two indices still don't match, swap the values
if (i <= j) {
swap(items, i, j);
// change indices to continue loop
i++;
j--;
}
}
// this value is necessary for recursion
return i;
}
/**
* A quicksort implementation in JavaScript. The array
* is sorted in place.
* @param {Array} items An array of items to sort.
* @return {Array} The sorted array.
*/
function quickSort(items, left, right) {
var index;
// performance - don't sort an array with zero or one items
if (items.length > 1) {
// fix left and right values - might not be provided
left = typeof left != "number" ? 0 : left;
right = typeof right != "number" ? items.length - 1 : right;
// split up the entire array
index = partition(items, left, right);
// if the returned index
if (left < index - 1) {
quickSort(items, left, index - 1);
}
if (index < right) {
quickSort(items, index, right);
}
}
return items;
}