forked from ManspergerMichael/Algorithims
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortByHeight.js
41 lines (39 loc) · 911 Bytes
/
sortByHeight.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
34
35
36
37
38
39
40
41
//bubblesort with unmoveable elements
function sortByHeight(a){
var array2 = a;
array2 = array2.filter((element)=>{
if(element != -1){
return element;
}
}).sort((a,b)=>{
return a-b;
});
var indexVal =0;
for(var i = 0; i < a.length; i++){
if(a[i] != -1){
a[i] = array2[indexVal];
indexVal++;
}
}
return array2;
}
var a = [-1,150,190,170,-1,-1,160,180]
console.log(sortByHeight(a));
//simple bubble sort
function sortByHeight2(a) {
for(var i = 0; i<a.length; i++){
for(var j = 1; j<a.length; j++){
if(a[j] == -1){
continue;
}
else{
if(a[j-1] > a[j]){
var temp = a[j-1];
a[j - 1] = a[j];
a[j] = temp;
}
}
}
}
return a;
}