-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.js
More file actions
39 lines (36 loc) · 1.32 KB
/
bubbleSort.js
File metadata and controls
39 lines (36 loc) · 1.32 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
// on 26th August 2024 [12:06 PM ]
// a simple function to return the length of an iterable eg an ARRAY, STRING, e.t.c
function count(iterable){
if(!isNaN(iterable)) iterable = `${iterable}`;
let i =0;
for(const item of iterable) i++;
return i;
}
/* the bubble sort logic function*/
function bubbleSort(array){
let iterations = sort(array), iterationsCount =[];
while(iterations.swaps > 0){
iterationsCount.push(iterations.swaps);
iterations = sort(iterations.array);
}
return [iterations.array, `Nos. of swaps made per Iteration: ${iterationsCount}`,
`Total iterations(Sort Pass) made to sort the List: ${count(iterationsCount)}`, `List of ${count(array)} elements`];
function sort (array){
let len = count(array), c=0;
for(let i=len; i>=0; i--){
if(array[i]<array[i-1]){
[array[i], array[i-1]] = [array[i-1], array[i]]; // swapping the items positions
c++;
}
}
return {swaps:c, array};
}
}
/**
* Testing the Algo with a normal unordered, ordered, reverse ordered lists
* and a list with only one misplaced element
*/
console.log(bubbleSort([22,3,44,5,6,8,7,10,9,15]));
console.log(bubbleSort([1,3,2,4,4,5,6,7,8,9]));
console.log(bubbleSort([13,12,11,9,8,7,6,5,3,2]));
console.log(bubbleSort([10,11,22,33,44,55,66,77,88,99]));