-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
252 lines (202 loc) · 9.3 KB
/
index.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Natural Merge Sort</title>
<style type="text/css">
html, body {
margin: 0px;
font-family: Arial, Verdana;
font-size: 20px;
text-align: center;
background-color: #F8F8F8;
}
.smooth_line {
color: #F8F8F8;
background-color: #F8F8F8;
width: 67%;
height: 0px;
}
</style>
<!--<script type="text/javascript" src="algorithm.js"></script>-->
</head>
<body>
<br>
<script type="text/javascript">
var MARGIN_HEIGHT = 11;
var colours = [ "red", "black" ];
var symbols = [ "♦", "♥", "♠", "♣" ];
var cards = [ "7", "8", "9", "10", "J", "Q", "K", "A" ];
var num_cards = symbols.length * cards.length;
/* */
function return_card (index) {
var colour_index = parseInt(index / (num_cards / colours.length));
var symbol_index = parseInt(index / (num_cards / symbols.length));
var card_index = index % cards.length;
var card = "<span style='color: " + colours[colour_index] + "'>" + symbols[symbol_index] + cards[card_index] + "</span>";
return card;
}
function return_formatted_card (obj, move_y) {
var margin = (move_y) ? obj.margin : 0;
return "<span style='position: relative; top: " + margin + "px'>" + return_card(obj.value) + "</span>";
}
function display_list (first, move_y) {
var content = return_formatted_card(first, move_y);
var tmp = first.next;
var space = " ";
while ( tmp ) {
content += space + return_formatted_card(tmp, move_y);
tmp = tmp.next;
}
return content;
}
function two_col_table () {
var table = document.createElement("table");
table.align = "center";
table.border = "0";
table.cellSpacing = "4";
var tr = document.createElement("tr");
table.appendChild(tr);
var td1 = document.createElement("td");
var td2 = document.createElement("td");
td1.align = "left";
td1.vAlign = "middle";
td2.vAlign = "middle";
tr.appendChild(td1);
tr.appendChild(td2);
return [ table, td1, td2 ];
}
var layer_counter = 0;
function new_layer () {
layer_counter++;
var colour = 250 - (2 * layer_counter);
var hex_colour = colour.toString(16);
var tmp = document.createElement("div");
tmp.style.backgroundColor = "#" + hex_colour + hex_colour + hex_colour;
layer = tmp;
document.body.appendChild(layer);
}
/* */
function create_indices (amount) {
var source = [];
var target = [];
var i, random_index;
for ( i = 0; i < amount; i++ )
source.push(i);
while ( source.length ) {
random_index = Math.floor(Math.random() * source.length);
target.push(source[random_index]);
source.splice(random_index, 1);
}
return target;
}
function create_container (the_value) {
var tmp = {};
tmp.value = the_value;
tmp.margin = 0;
tmp.next = null;
return tmp;
}
function create_list (start, values) {
var tmp = start;
var next;
for ( var i = 1; i < values.length; i++ ) {
next = create_container(values[i]);
tmp.next = next;
tmp = next;
}
return tmp;
}
function merge (first, second) {
var to_return, alternative, tmp_alternative;
if ( first.value <= second.value ) {
to_return = first;
alternative = second;
}
else {
to_return = second;
alternative = first;
}
var tmp = to_return;
while ( alternative ) {
while ( tmp.next && tmp.next.value <= alternative.value )
tmp = tmp.next;
tmp_alternative = tmp.next;
tmp.next = alternative;
tmp = alternative;
alternative = tmp_alternative;
}
return to_return;
}
function natural_merge_sort (first, last) {
var first_end = first;
if ( remembered_first == first_end )
new_layer();
first_end.margin = 0 - MARGIN_HEIGHT;
while ( first_end.next && first_end.value <= first_end.next.value ) {
first_end = first_end.next;
if ( remembered_first == first_end )
new_layer();
first_end.margin = 0 - MARGIN_HEIGHT;
}
if ( !first_end.next )
return first;
layer.innerHTML += "<hr class='smooth_line'>";
var second_start = first_end.next;
var second_end = second_start;
if ( remembered_first == second_end )
new_layer();
second_end.margin = MARGIN_HEIGHT;
first_end.next = null;
while ( second_end.next && second_end.value <= second_end.next.value ) {
second_end = second_end.next;
if ( remembered_first == second_end )
new_layer();
second_end.margin = MARGIN_HEIGHT;
}
var remaining = second_end.next;
second_end.next = null;
var space = " ⋅ ";
var first_list = display_list(first, 0);
var second_list = display_list(second_start, 0);
var remaining_list = (remaining) ? display_list(remaining, 0) : "";
var content = first_list + space + second_list + space + remaining_list + "<br>";
layer.innerHTML += content;
var table = two_col_table();
table[1].innerHTML = first_list + "<br>" + second_list;
table[2].innerHTML = space + remaining_list;
layer.appendChild(table[0]);
var sorted = merge(first, second_start);
table = two_col_table();
table[0].style.marginTop = MARGIN_HEIGHT + "px";
table[0].style.marginBottom = parseInt(1.5 * MARGIN_HEIGHT) + "px";
table[1].innerHTML = display_list(sorted, 1);
table[2].innerHTML = space + remaining_list;
layer.appendChild(table[0]);
var sorted_list = display_list(sorted, 0);
content = sorted_list + space + remaining_list + "<br>";
layer.innerHTML += content;
if ( !remaining )
return sorted;
content = "<div style='margin-top: " + MARGIN_HEIGHT + "px'>" + remaining_list + space + sorted_list + "</div>";
layer.innerHTML += content;
last.next = sorted;
while ( last.next )
last = last.next;
return natural_merge_sort(remaining, last);
}
var values = create_indices(num_cards);
var first = create_container(values[0]);
var last = create_list(first, values);
values = [];
var layer = document.body;
layer.innerHTML += display_list(first, 0);
var remembered_first = first;
var sorted = natural_merge_sort(first, last);
layer = document.body;
layer.innerHTML += "<hr class='smooth_line'>";
layer.innerHTML += display_list(sorted, 0);
</script>
</body>
</html>