forked from RhythmPahwa14/AlgoVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperformance-algorithms-fix.patch
More file actions
145 lines (141 loc) · 8.36 KB
/
performance-algorithms-fix.patch
File metadata and controls
145 lines (141 loc) · 8.36 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
From aaf7831e272075991b07dc7fa1e1206abea1324a Mon Sep 17 00:00:00 2001
From: User <user@example.com>
Date: Mon, 27 Oct 2025 22:17:30 +0530
Subject: [PATCH] Fix: Clean up duplicate implementations and complete
performance algorithms
---
src/algorithms/performanceAlgorithms.js | 111 +-----------------------
1 file changed, 3 insertions(+), 108 deletions(-)
diff --git a/src/algorithms/performanceAlgorithms.js b/src/algorithms/performanceAlgorithms.js
index 34f1220..4383064 100644
--- a/src/algorithms/performanceAlgorithms.js
+++ b/src/algorithms/performanceAlgorithms.js
@@ -282,108 +282,6 @@ export const bucketSortPerformance = (arr) => {
return result;
};
-export const cocktailShakerSortPerformance = (arr) => {
- const array = [...arr];
- const n = array.length;
- let swapped = true;
- let start = 0;
- let end = n - 1;
-
- while (swapped) {
- swapped = false;
-
- // Forward pass
- for (let i = start; i < end; i++) {
- if (array[i] > array[i + 1]) {
- [array[i], array[i + 1]] = [array[i + 1], array[i]];
- swapped = true;
- }
- }
-
- if (!swapped) break;
-
- end--;
- swapped = false;
-
- // Backward pass
- for (let i = end; i > start; i--) {
- if (array[i] < array[i - 1]) {
- [array[i], array[i - 1]] = [array[i - 1], array[i]];
- swapped = true;
- }
- }
-
- start++;
- }
-
- return array;
-};
-
-export const countingSortPerformance = (arr) => {
- if (arr.length <= 1) return [...arr];
-
- const array = [...arr];
- const max = Math.max(...array);
- const min = Math.min(...array);
- const range = max - min + 1;
- const count = new Array(range).fill(0);
- const output = [];
-
- // Store count of each element
- for (let i = 0; i < array.length; i++) {
- count[array[i] - min]++;
- }
-
- // Build the output array
- for (let i = 0; i < range; i++) {
- while (count[i]-- > 0) {
- output.push(i + min);
- }
- }
-
- return output;
-};
-
-export const heapSortPerformance = (arr) => {
- const array = [...arr];
- const n = array.length;
-
- // Build max heap
- for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
- heapify(array, n, i);
- }
-
- // Extract elements from heap one by one
- for (let i = n - 1; i > 0; i--) {
- [array[0], array[i]] = [array[i], array[0]]; // Move current root to end
- heapify(array, i, 0); // Call heapify on the reduced heap
- }
-
- return array;
-};
-
-function heapify(array, n, i) {
- let largest = i; // Initialize largest as root
- const left = 2 * i + 1;
- const right = 2 * i + 2;
-
- // If left child is larger than root
- if (left < n && array[left] > array[largest]) {
- largest = left;
- }
-
- // If right child is larger than largest so far
- if (right < n && array[right] > array[largest]) {
- largest = right;
- }
-
- // If largest is not root
- if (largest !== i) {
- [array[i], array[largest]] = [array[largest], array[i]]; // Swap
- heapify(array, n, largest); // Recursively heapify the affected sub-tree
- }
-}
-
export const introSortPerformance = (arr) => {
const array = [...arr];
const n = array.length;
@@ -750,15 +648,12 @@ export const performanceAlgorithms = {
"Selection Sort": selectionSortPerformance,
"Quick Sort": quickSortPerformance,
"Merge Sort": mergeSortPerformance,
- "Cocktail Shaker Sort": cocktailShakerSortPerformance, // ✅ Added
- "Counting Sort": countingSortPerformance, // ✅ Added
- "Heap Sort": heapSortPerformance, // ✅ Added
- "Linear Search": linearSearchPerformance,
- "Binary Search": binarySearchPerformance,
- "Bucket Sort": bucketSortPerformance,
"Cocktail Shaker Sort": cocktailShakerSortPerformance,
"Counting Sort": countingSortPerformance,
"Heap Sort": heapSortPerformance,
+ "Linear Search": linearSearchPerformance,
+ "Binary Search": binarySearchPerformance,
+ "Bucket Sort": bucketSortPerformance,
"Intro Sort": introSortPerformance,
"Jump Search": jumpSearchPerformance,
"Radix Sort": radixSortPerformance,
--
2.49.0.windows.1