-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg124.js
83 lines (71 loc) · 1.62 KB
/
pg124.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
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
//pg124.js
//chapter 12 - sorts
//array - bubble sort
var aBubbleSort = function(arr){
for(var i=0; i<=arr.length-1; i++){
var endAt = arr.length-i-1;
for(var j=0; j<=endAt; j++){
if(arr[j] > arr[j+1]){
var temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
//console.log(aBubbleSort([7,3,7,5,4,2,9]))
//array - selection sort
var aSelectionSort = function(arr){
for(var i=0;i<arr.length;i++){
var smallest = arr[i];
var sLoc = i;
for(var j=i; j<arr.length; j++){
if(smallest>arr[j]){
smallest = arr[j];
sLoc = j;
}
}
var temp = arr[i];
arr[i] = smallest;
arr[sLoc] = temp;
};
return arr;
}
//console.log(aSelectionSort([7,3,7,5,4,2,9]))
//merge sort part 1 - merges 2 ALREADY SORTED arrays
var merge = function(left, right){
var lLoc = rLoc = 0;
var result = [];
while(lLoc < left.length || rLoc < right.length){
if(left[lLoc] < right[rLoc]){
result.push(left[lLoc])
// if(lLoc<left.length){
// lLoc++;
// }
lLoc++
}
else{
result.push(right[rLoc])
// if(rLoc<right.length){
// rLoc++;
// }
rLoc++
}
}
var returnThis = result.concat(left.slice(lLoc).concat(right.slice(rLoc)));
console.log('return this', returnThis)
return result;
}
//merge sort 2 - recursively breaks down arrays into single elements
var mergeSort = function(arr){
if(arr.length<2){
return arr;
}
var middle = Math.floor((arr.length)/2);
var right = arr.slice(middle,arr.length);
var left = arr.slice(0, middle);
console.log('left', left, 'right', right)
return merge(mergeSort(left), mergeSort(right));
}
console.log(mergeSort([3,6,4,2,4,5]))