forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.ts
67 lines (53 loc) · 1.34 KB
/
Quick_Sort.ts
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
// Description: Function to perform the Quick Sort algorithm on an array of numbers
// Expected Output: returns the sorted array
// Helper function to get the Pivoting Element Index
function partition(array: number[], left: number = 0, right: number = array.length - 1)
{
const pivot = array[Math.floor((right + left) / 2)];
let i = left;
let j = right;
while (i <= j)
{
while (array[i] < pivot)
{
i++;
}
while (array[j] > pivot)
{
j--;
}
// Swap values based on indices
if (i <= j)
{
[array[i], array[j]] = [array[j], array[i]];
i++;
j--;
}
}
return i;
}
// Function that uses recursive definition for quick sort implementation
function quickSort(array: number[], left: number = 0, right: number = array.length - 1)
{
let index;
if (array.length > 1)
{
index = partition(array, left, right);
if (left < index - 1)
{
quickSort(array, left, index - 1);
}
if (index < right)
{
quickSort(array, index, right);
}
}
return array;
}
// I/P
var arr = [6, 5, 4, 3, 2, 1];
console.log("Before sorting ",arr)
var res = quickSort(arr)
console.log("After sorting ",res)
// O/P
// 1,2,3,4,5,6