forked from super30admin/PreCourse-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise_4.js
34 lines (33 loc) · 849 Bytes
/
Exercise_4.js
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
class MergeSort {
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
function merge(arr, l, m, r) {
//Your code here
}
// Main function that sorts arr[l..r] using
// merge()
function sort(arr, l, r) {
//Write your code here
//Call mergeSort from here
}
/* A utility function to print array of size n */
function printArray(arr) {
let n = arr.length;
for (let i = 0; i < n; ++i)
console.log(arr[i] + " ");
console.log();
}
}
// Driver method
let arr = [12, 11, 13, 5, 6, 7];
console.log("Given Array");
let ob = new MergeSort();
ob.printArray(arr);
ob.sort(arr, 0, arr.length - 1);
console.log("\nSorted array");
ob.printArray(arr);