-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge_sort.js
More file actions
executable file
·105 lines (77 loc) · 2.69 KB
/
merge_sort.js
File metadata and controls
executable file
·105 lines (77 loc) · 2.69 KB
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
#!/usr/bin/env node --harmony
function mergeSort(array) {
// console.log('Merge Sorting Array:');
// console.log(array);
var working_arrays = [];
var sorted_array = [];
var final_length = array.length;
var next_item;
// push each item into an Array of Arrays, the working array
while(array.length > 0) {
next_item = array.pop();
working_arrays.push([next_item]);
}
// console.log("remaining: " + array.length)
// now we have individual items like this:
// [n], [n], [n], [n], [n], [n], [n], [n]
//
// and we're going to iteratively make sorted arrays like this:
// [n,n], [n,n], [n,n], [n,n]
//
// then:
// [n,n,n,n], [n,n,n,n]
//
// and finally:
// [n,n,n,n,n,n,n,n]
//
var merged_array = [];
// until the final, sorted array is of the expected length
while(working_arrays.length > 0) {
// pop sub-arrays of the working array
var first_array = working_arrays.pop();
var second_array = working_arrays.pop();
// console.log('first array, length ' + first_array.length + ', items are : ' + first_array);
// console.log('second array, length ' + second_array.length + ', items are : ' + second_array);
while((first_array.length > 0) || (second_array.length > 0)) {
// compare single items at a time, drawing inwards as we go
var left_item = first_array[0];
// console.log('left item: ' + left_item);
var right_item = second_array[0];
// console.log('right item: ' + right_item);
// i prefer for clarity to handle these undefined states individually
if (typeof left_item == 'undefined') {
// console.log('pushing: ' + right_item);
merged_array.push(second_array.shift());
} else if (typeof right_item == 'undefined') {
// console.log('pushing: ' + left_item);
merged_array.push(first_array.shift());
} else if (left_item < right_item) {
// console.log('pushing: ' + left_item);
merged_array.push(first_array.shift());
} else {
// console.log('pushing: ' + right_item);
merged_array.push(second_array.shift());
}
// console.log('so far: ' + merged_array)
// console.log("\n")
}
if (merged_array.length == final_length) {
} else {
working_arrays.unshift(merged_array);
merged_array = [];
}
}
return merged_array;
}
// read buffer
var rawInput = '';
process.stdin.resume();
process.stdin.on('data', function(chunk) {
rawInput += chunk.toString()
});
// nothing else to read, fire
process.stdin.on('end', function() {
// split and cast to Numbers
var input = rawInput.split(',').map(function(i){ return Number(i) });
process.stdout.write(mergeSort(input) + '\n');
});