-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.html
93 lines (80 loc) · 2.56 KB
/
merge.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title></title>
<style>
body {padding-top:50px ; text-align: center;; margin-top: auto;background-color:#0c7b93}
div {color: white; }
</style>
</head>
<body>
<h1>MERGE SORT</h1>
<button onclick="location.href='mergecode.html'">See Merge Sort C++ Code</button> <br /><br />
<button onclick="location.href='index.html'">Go to Home Page</button> <br /><br />
<input placeholder="ADD ONE NUMBER AT A TIME" type="text" id="text1" size="30"></input>
<input type="button" id="button1" value="ADD" onclick="add_element_to_array(); disp()"></input><br><br>
<input type="button" id="button2" value="SORT" onclick="display_array();"><br /><br />
<button onclick="location.href='merge.html'">RESET</button><br />
<div id="Temp">
</div>
<div id="Result"></div>
<script type="text/javascript">
var x = 0;
var array = Array();
function add_element_to_array()
{
array[x] = parseFloat(document.getElementById("text1").value);
// alert("Element: " + array[x] + " Added at index " + x);
x++;
document.getElementById("text1").value = "";
}
function disp(){
var t="<hr/>";
for(var i=0;i<array.length;i++){
t += "Element " + i + " = " + array[i] + "<br/>";
}
document.getElementById("Temp").innerHTML = t;
}
function mergeSort(list){
if(list.length <= 1) return list;
const middle = list.length / 2 ;
const left = list.slice(0, middle);
const right = list.slice(middle, list.length);
return merge(mergeSort(left), mergeSort(right));
}
function merge (left, right){
var result = [];
while(left.length || right.length) {
if(left.length && right.length) {
if(left[0] < right[0]) {
result.push(left.shift())
} else {
result.push(right.shift())
}
} else if(left.length) {
result.push(left.shift())
} else {
result.push(right.shift())
}
}
return result;
}
function display_array()
{
var e = "<hr/>";
var start = performance.now();
var arr = mergeSort(array);
var end = performance.now();
var time = end - start;
e+="SORTED BY MERGE SORT"+"<br />"
for (var y=0; y<arr.length; y++)
{
e += "Element " + y + " = " + arr[y] + "<br/>";
}
e+="TIME COMPLEXITY = O(NLOG(N))"+"<br/>"+"TIME TAKEN = "+time+"ms<br/>";
document.getElementById("Result").innerHTML = e;
}
</script>
</body>
</html>