Skip to content

Commit 06ee495

Browse files
authored
Create mergeSort.js
1 parent fa84e4c commit 06ee495

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

Diff for: mergeSort.js

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//Solving MergeSort Algorithm in Javascript
2+
3+
4+
5+
// Canvas variables
6+
var canvas, canvaswidth, canvasheight, ctrl;
7+
8+
// Call canvasElements() to store height width
9+
// in above canvas variables
10+
canvasElements();
11+
12+
// 3 array are declared
13+
14+
//1) arr is for storing array element
15+
//2) itmd for storing intermediate values
16+
//3) visited is for element which has been sorted
17+
var arr = [], itmd = [], visited = []
18+
19+
20+
// Length of unsorted array
21+
var len_of_arr = 40;
22+
23+
// Store random value in arr[]
24+
for (var i = 0; i < len_of_arr; i++) {
25+
arr.push(Math.round(Math.random() * 250) )
26+
}
27+
28+
// Initialize itmd and visited array with 0
29+
for (var i = 0; i < len_of_arr; i++) {
30+
itmd.push(0)
31+
visited.push(0)
32+
}
33+
34+
// Merging of two sub array
35+
// https://www.geeksforgeeks.org/merge-two-sorted-arrays/
36+
function mergeArray(start, end) {
37+
let mid = parseInt((start + end) >> 1);
38+
let start1 = start, start2 = mid + 1
39+
let end1 = mid, end2 = end
40+
41+
// Initial index of merged subarray
42+
let index = start
43+
44+
while (start1 <= end1 && start2 <= end2) {
45+
if (arr[start1] <= arr[start2]) {
46+
itmd[index] = arr[start1]
47+
index = index + 1
48+
start1 = start1 + 1;
49+
}
50+
else if(arr[start1] > arr[start2]) {
51+
itmd[index] = arr[start2]
52+
index = index + 1
53+
start2 = start2 + 1;
54+
}
55+
}
56+
57+
// Copy the remaining elements of
58+
// arr[], if there are any
59+
while (start1 <= end1) {
60+
itmd[index] = arr[start1]
61+
index = index + 1
62+
start1 = start1 + 1;
63+
}
64+
65+
while (start2 <= end2) {
66+
itmd[index] = arr[start2]
67+
index = index + 1
68+
start2 = start2 + 1;
69+
}
70+
71+
index = start
72+
while (index <= end) {
73+
arr[index] = itmd[index];
74+
index++;
75+
}
76+
}
77+
78+
// Function for showing visualization
79+
// effect
80+
function drawBars(start, end) {
81+
82+
// Clear previous unsorted bars
83+
ctrl.clearRect(0, 0, 1000, 1500)
84+
85+
// Styling of bars
86+
for (let i = 0; i < len_of_arr; i++) {
87+
88+
// Changing styles of bars
89+
ctrl.fillStyle = "black"
90+
ctrl.shadowOffsetX = 2
91+
ctrl.shadowColor = "chocolate";
92+
ctrl.shadowBlur = 3;
93+
ctrl.shadowOffsetY =5;
94+
95+
96+
// Size of rectangle of bars
97+
ctrl.fillRect(25 * i, 300 - arr[i], 20, arr[i])
98+
99+
if (visited[i]) {
100+
ctrl.fillStyle = "#006d13"
101+
ctrl.fillRect(25 * i, 300 - arr[i], 20, arr[i])
102+
ctrl.shadowOffsetX = 2
103+
}
104+
}
105+
106+
for (let i = start; i <= end; i++) {
107+
ctrl.fillStyle = "orange"
108+
ctrl.fillRect(25 * i, 300 - arr[i], 18, arr[i])
109+
ctrl.fillStyle = "#cdff6c"
110+
ctrl.fillRect(25 * i,300, 18, arr[i])
111+
visited[i] = 1
112+
}
113+
}
114+
115+
// Waiting interval between two bars
116+
function timeout(ms) {
117+
return new Promise(resolve => setTimeout(resolve, ms));
118+
}
119+
120+
121+
// Merge Sorting
122+
const mergeSort = async (start, end) => {
123+
if (start < end) {
124+
let mid = parseInt((start + end) >> 1)
125+
await mergeSort(start, mid)
126+
await mergeSort(mid + 1, end)
127+
await mergeArray(start, end)
128+
await drawBars(start, end)
129+
130+
// Waiting time is 800ms
131+
await timeout(800)
132+
}
133+
}
134+
135+
// canvasElements function for storing
136+
// width and height in canvas variable
137+
function canvasElements() {
138+
canvas = document.getElementById("Canvas")
139+
canvas.width = canvas.height = 1000
140+
canvaswidth = canvas.width
141+
canvasheight = canvas.height
142+
ctrl = canvas.getContext("2d")
143+
}
144+
145+
// Asynchronous MergeSort function
146+
const performer = async () => {
147+
await mergeSort(0, len_of_arr - 1)
148+
await drawBars()
149+
150+
// Code for change title1 text
151+
const title1_changer = document.querySelector(".title1")
152+
title1_changer.innerText = "Array is completely sorted"
153+
}
154+
performer()

0 commit comments

Comments
 (0)