Skip to content

Commit fab2f15

Browse files
committed
quick sort
1 parent af188ca commit fab2f15

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

Sorting/quick-sort-Hoare.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// First write the swap function, which is just a helper function to swap values of the array.
2+
function swap(array, i, j) {
3+
var temp = array[i];
4+
array[i] = array[j];
5+
array[j] = temp;
6+
}
7+
8+
function quicksortHoare(array, left, right) {
9+
// left-pointer would be the index of the first element which is 0 and right-pointer would be the index of the last element which would be (length -1).
10+
left = left || 0;
11+
right = right || array.length - 1;
12+
13+
var pivot = partitionHoare(array, left, right);
14+
15+
if (left < pivot - 1) {
16+
quicksortHoare(array, left, pivot - 1);
17+
}
18+
19+
if (right > pivot) {
20+
quicksortHoare(array, pivot, right)
21+
}
22+
23+
return array;
24+
25+
}
26+
27+
/* Two indices that start at the ends of the array being partitioned, then move toward each other, until they detect an inversion: a pair of elements, one greater than the pivot, one smaller, that are in the wrong order relative to each other. The inverted elements are then swapped.
28+
Here the numerical values of left and right is continually getting updated with each inner while loop. But only if the while loop condition gets satisfied. That is, when the while loop condition is unsatisfied, e.g. for the first inner while loop, when array[left] > array[pivot] which means we have found a misplaced pair.
29+
30+
That is, although the left <= right (which is being made sure by the outer while loop) the actual elements are not sorted. Meaning a left side element is larger in value than the right side element. So, the code execution then jumps out of the inner while loop and goes right in to execute the swap function.
31+
*/
32+
function partitionHoare(array, left, right) {
33+
var pivot = Math.floor((left + right) / 2);
34+
35+
while (left < right) {
36+
while (array[left] < array[pivot]) {
37+
left++
38+
}
39+
while (array[right] > array[pivot]) {
40+
right--
41+
}
42+
43+
if (left <= right) {
44+
swap(array, left, right);
45+
left++
46+
right--
47+
}
48+
}
49+
return left;
50+
}
51+
52+
53+
54+
/******************* Testing Hoare algorithm *********************/
55+
56+
function getRandomInt(min, max) {
57+
return Math.floor(Math.random() * (max - min + 1)) + min;
58+
// By adding 1, I am making the maximum inclusive ( the minimum is inclusive anyway). Because, the Math.random() function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1
59+
}
60+
61+
var arr = [];
62+
63+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
64+
arr.push(getRandomInt(1, 100));
65+
}
66+
67+
console.log("Unsorted array: ");
68+
console.log(arr); //printing unsorted array
69+
70+
arr = quicksortHoare(arr, 0, arr.length - 1);
71+
console.log("Sorted array: ");
72+
console.log(arr);

Sorting/quick-sort-Lomuto.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// First write the swap function, which is just a helper function to swap values of the array.
2+
function swap(array, i, j) {
3+
var temp = array[i];
4+
array[i] = array[j];
5+
array[j] = temp;
6+
}
7+
8+
function quicksortLomuto(array, left, right) {
9+
// left-pointer would be the index of the first element which is 0 and right-pointer would be the index of the last element which would be (length -1).
10+
left = left || 0;
11+
right = right || array.length - 1;
12+
13+
var pivot = partitionLomuto(array, left, right);
14+
15+
if (left < pivot - 1) {
16+
quicksortLomuto(array, left, pivot - 1);
17+
}
18+
19+
if (right > pivot) {
20+
quicksortLomuto(array, pivot - 1, right)
21+
}
22+
23+
return array;
24+
}
25+
26+
function partitionLomuto(array, left, right) {
27+
// Lomuto algorithm always uses the last element, array[right], for the pivot.
28+
var pivot = right;
29+
var i = left;
30+
31+
/*The logic under Lomuto is, we start from the leftmost element and keep track of index of smaller (or equal to) elements as j. While traversing, if we find a smaller element, we swap current element with arr[j]. Otherwise we ignore current element.*/
32+
for (var j = left; j < right; j++) {
33+
if (array[j] <= array[pivot]) {
34+
swap(array, i, j);
35+
i++
36+
}
37+
}
38+
swap(array, i, j);
39+
return i;
40+
}
41+
42+
/******************* Testing Lomuto algorithm *********************/
43+
44+
function getRandomInt(min, max) {
45+
return Math.floor(Math.random() * (max - min + 1)) + min;
46+
// By adding 1, I am making the maximum inclusive ( the minimum is inclusive anyway). Because, the Math.random() function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1
47+
}
48+
49+
var arr = [];
50+
51+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
52+
arr.push(getRandomInt(1, 100));
53+
}
54+
55+
console.log("Unsorted array: ");
56+
console.log(arr); //printing unsorted array
57+
58+
arr = quicksortLomuto(arr, 0, arr.length - 1);
59+
console.log("Sorted array: ");
60+
console.log(arr);

Sorting/quick-sort-basic.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// sorting the below array with quick sorting
2+
3+
/*The algorithm for Quicksort is:
4+
1. Pick a pivot element that divides the list into two sublists.
5+
6+
2. Reorder the list so that all elements less than the pivot element are placed before
7+
the pivot and all elements greater than the pivot are placed after it.
8+
9+
3. Repeat steps 1 and 2 on both the list with smaller elements and the list of larger
10+
elements.*/
11+
12+
// basic implementation, where pivot is the first element, without usuing swap function and partition function.
13+
14+
function quickSortBasic(array) {
15+
if(array.length < 2) {
16+
return array;
17+
}
18+
19+
var pivot = array[0];
20+
var lesserArray = [];
21+
var greaterArray = [];
22+
23+
for (var i = 1; i < array.length; i++) {
24+
if ( array[i] > pivot ) {
25+
greaterArray.push(array[i]);
26+
} else {
27+
lesserArray.push(array[i]);
28+
}
29+
}
30+
31+
return quickSortBasic(lesserArray).concat(pivot, quickSortBasic(greaterArray));
32+
}
33+
34+
35+
/******************* Testing Quick sort algorithm *********************/
36+
37+
// Returns a random integer between min (inclusive) and max (inclusive). Using Math.round() will give a non-uniform distribution, which we dont want in this case.
38+
39+
function getRandomInt(min, max) {
40+
return Math.floor(Math.random() * (max - min + 1)) + min;
41+
// By adding 1, I am making the maximum inclusive ( the minimum is inclusive anyway). Because, the Math.random() function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1
42+
}
43+
44+
var arr = [];
45+
46+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
47+
arr.push(getRandomInt(1, 100));
48+
}
49+
50+
console.log("Unsorted array: ");
51+
console.log(arr); //printing unsorted array
52+
53+
arr = quickSortBasic(arr, 0, arr.length - 1);
54+
console.log("Sorted array: ");
55+
console.log(arr);

0 commit comments

Comments
 (0)