-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.js
More file actions
117 lines (68 loc) · 1.69 KB
/
Copy pathsorting.js
File metadata and controls
117 lines (68 loc) · 1.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
function bubbleSort ( arr) {
var results = arr;
results.__proto__.compares = 0;
results.__proto__.swaps = 0;
if(arr.length == 1){
results.__proto__.compares = 0;
results.__proto__.swaps = 0;
return results;
}
var swaps = 1;
while(swaps > 0){
swaps = 0;
for(var i = 0; i < (arr.length - 1); i++){
results.__proto__.compares++;
if(results[i] > results[i+1]){
var first = results[i+1];
var second = results[i];
results[i] = first;
results[i+1] = second;
results.__proto__.swaps++;
swaps++;
}
}
}
return results;
}
//////////////////////////////////////////////
function merge (param1, param2) {
var results = [];
while(param1.length > 0 || param2.length > 0){
if(param1.length > 0 && param2.length > 0){
if(param1[0] <= param2[0])
results.push(param1.shift());
else
results.push(param2.shift());
}
else{
if(param2.length > 0 && param1.length == 0)
results.push(param2.shift());
if(param1.length > 0 && param2.length == 0)
results.push(param1.shift());
}
}
return results;
}
///////////////////////////////////////////
function split(param){
var half = Math.ceil(param.length/2);
return ([param.slice(0,half), param.slice(half)]);
}
//////////////////////////////////////////////////
function mergeSort(arr){
if(arr.length<2) return arr;
var left = split(arr)[0];
var right = split(arr)[1];
return merge(mergeSort(left), mergeSort(right));
}
// if(arr.length<=1){
// console.log(left,right)
// return arr;
// }
// else{
// console.log(left, right);
// mergeSort(left);
// mergeSort(right);
// }
// console.log(left,right)
// return merge(left,right);