Skip to content

Commit 45100a1

Browse files
committed
Added merge sort without color correction on the bar
1 parent ff740ab commit 45100a1

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

function.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,85 @@ const SortAlgo = {
150150
sort(this);
151151
},
152152

153+
//Merge Sort methods to perform merge sort algorithm
154+
mergeSort() {
155+
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
156+
157+
// async function for selection sort algorithm
158+
async function sort(self) {
159+
function merge(arr, l, m, r) {
160+
var n1 = m - l + 1;
161+
var n2 = r - m;
162+
163+
// Create temp arrays
164+
var L = new Array(n1);
165+
var R = new Array(n2);
166+
167+
// Copy data to temp arrays L[] and R[]
168+
for (var i = 0; i < n1; i++) L[i] = arr[l + i];
169+
for (var j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
170+
171+
// Merge the temp arrays back into arr[l..r]
172+
173+
// Initial index of first subarray
174+
var i = 0;
175+
176+
// Initial index of second subarray
177+
var j = 0;
178+
179+
// Initial index of merged subarray
180+
var k = l;
181+
182+
while (i < n1 && j < n2) {
183+
if (L[i] <= R[j]) {
184+
arr[k] = L[i];
185+
i++;
186+
} else {
187+
arr[k] = R[j];
188+
j++;
189+
}
190+
k++;
191+
}
192+
193+
// Copy the remaining elements of
194+
// L[], if there are any
195+
while (i < n1) {
196+
arr[k] = L[i];
197+
i++;
198+
k++;
199+
}
200+
201+
// Copy the remaining elements of
202+
// R[], if there are any
203+
while (j < n2) {
204+
arr[k] = R[j];
205+
j++;
206+
k++;
207+
}
208+
}
209+
210+
// l is for left index and r is
211+
// right index of the sub-array
212+
// of arr to be sorted */
213+
function mergeSort(arr, l, r) {
214+
if (l >= r) {
215+
return; //returns recursively
216+
}
217+
var m = l + parseInt((r - l) / 2);
218+
mergeSort(arr, l, m);
219+
mergeSort(arr, m + 1, r);
220+
merge(arr, l, m, r);
221+
}
222+
mergeSort(data, 0, data.length - 1);
223+
swapBar(data);
224+
isSorting = false;
225+
isSorted = true;
226+
togglePlay();
227+
}
228+
// calling sort function here
229+
sort(this);
230+
},
231+
153232
// If user wants to stop the sorting process then this function will be called and sorting algorithm will be stopped immediately.
154233
sortStop() {
155234
this.abort = true;
@@ -168,6 +247,9 @@ function startSorting() {
168247
} else if (getAlgo() == "selection-sort") {
169248
const selectionSortStarted = SortAlgo.selectionSort.bind(SortAlgo);
170249
selectionSortStarted();
250+
} else if (getAlgo() == "merge-sort") {
251+
const mergeSortStarted = SortAlgo.mergeSort.bind(SortAlgo);
252+
mergeSortStarted();
171253
}
172254
}
173255

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ <h1>Sorting Algorithm Visualizer</h1>
3232
value="selection-sort"
3333
/>Selection Sort</label
3434
>
35+
<label>
36+
<input type="radio" name="algo-name" value="merge-sort" />Merge
37+
Sort</label
38+
>
3539

3640
<br />
3741
<br />

0 commit comments

Comments
 (0)