-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortTheArray.js
62 lines (54 loc) · 1.64 KB
/
sortTheArray.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
/* Find the maximum value or sort the values in an array using BubbleSort */
sortTheArrayDesc = (x) => {
let myArray = new Array();
myArray = x;
// console.log(myArray.length);
for(let i = 0; i<myArray.length-1; i++){
for(let j = 0; j<myArray.length;j++){
let initialVal = myArray[j]
let comparingVal = myArray[j+1];
if(initialVal < comparingVal){
temp = comparingVal;
myArray[j] = temp;
myArray[j+1] = initialVal;
// console.log(comparingVal + ":" + initialVal);
}
}
}
console.log(myArray);
}
sortTheArrayDesc([12,34,56,11,10]);
sortTheArrayDesc([120,342,56,11,110]);
sortTheArrayDesc([122,34,560,11,11]);
console.log("------ NEXT --------");
sortTheArrayAsc = (y) => {
let myArray = new Array();
myArray = y;
// console.log(myArray.length);
for(let i = 0; i<myArray.length-1; i++){
for(let j = 0; j<myArray.length;j++){
let initialVal = myArray[j]
let comparingVal = myArray[j+1];
if(initialVal > comparingVal){
temp = comparingVal;
myArray[j] = temp;
myArray[j+1] = initialVal;
// console.log(comparingVal + ":" + initialVal);
}
}
}
console.log(myArray);
}
sortTheArrayAsc([12,34,56,11,10]);
sortTheArrayAsc([120,342,56,11,110]);
sortTheArrayAsc([122,34,560,11,11]);
/* OUTPUT */
/*
[ 56, 34, 12, 11, 10 ]
[ 342, 120, 110, 56, 11 ]
[ 560, 122, 34, 11, 11 ]
------ NEXT --------
[ 10, 11, 12, 34, 56 ]
[ 11, 56, 110, 120, 342 ]
[ 11, 11, 34, 122, 560 ]
*/