forked from super30admin/PreCourse-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_5.js
More file actions
71 lines (59 loc) · 1.82 KB
/
Exercise_5.js
File metadata and controls
71 lines (59 loc) · 1.82 KB
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
class IterativeQuickSort {
// Swap without using a temporary variable
swap(arr, i, j) {
if (i !== j) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
}
// Lomuto partition scheme
partition(arr, l, h) {
let pivot = arr[h]; // Last element as pivot
let i = l - 1; // Pointer for smaller element
for (let j = l; j < h; j++) {
if (arr[j] < pivot) {
i++;
this.swap(arr, i, j); // Swap smaller element to correct position
}
}
this.swap(arr, i + 1, h); // Move pivot to correct position
return i + 1;
}
// Iterative QuickSort using stack
QuickSort(arr, l, h) {
let stack = []; // Stack to simulate recursion
// Push initial values of l and h
stack.push(l);
stack.push(h);
while (stack.length > 0) {
// Pop h and l
h = stack.pop();
l = stack.pop();
// Partition index
let p = this.partition(arr, l, h);
// Push left subarray to stack if it has more than one element
if (p - 1 > l) {
stack.push(l);
stack.push(p - 1);
}
// Push right subarray to stack if it has more than one element
if (p + 1 < h) {
stack.push(p + 1);
stack.push(h);
}
}
}
// Utility function to print array
printArr(arr, n) {
console.log(arr.join(" "));
}
}
// Driver code
let ob = new IterativeQuickSort();
let arr = [4, 3, 5, 2, 1, 3, 2, 3];
console.log("Given Array:");
ob.printArr(arr, arr.length);
ob.QuickSort(arr, 0, arr.length - 1);
console.log("Sorted Array:");
ob.printArr(arr, arr.length);