|
1 | | -/*Bubble Sort is a algorithm to sort an array. It |
| 1 | +/* Bubble Sort is a algorithm to sort an array. It |
2 | 2 | * compares adjacent element and swaps thier position |
3 | | -* The big O on bubble sort in worst and best case is O(N^2). |
| 3 | +* The big O on bubble sort in worst and best case is O(N^2). |
4 | 4 | * Not efficient. |
5 | 5 | */ |
6 | | -function bubbleSort(items) { |
7 | | - let length = items.length; |
8 | | - for (let i = (length - 1); i > 0; i--) { |
9 | | - //Number of passes |
10 | | - for (let j = (length - i); j > 0; j--) { |
11 | | - //Compare the adjacent positions |
12 | | - if (items[j] < items[j - 1]) { |
13 | | - //Swap the numbers |
14 | | - let tmp = items[j]; |
15 | | - items[j] = items[j - 1]; |
16 | | - items[j - 1] = tmp; |
17 | | - } |
18 | | - } |
| 6 | + |
| 7 | +function bubbleSort (items) { |
| 8 | + const length = items.length |
| 9 | + for (let i = (length - 1); i > 0; i--) { |
| 10 | + // Number of passes |
| 11 | + for (let j = (length - i); j > 0; j--) { |
| 12 | + // Compare the adjacent positions |
| 13 | + if (items[j] < items[j - 1]) { |
| 14 | + // Swap the numbers |
| 15 | + const tmp = items[j] |
| 16 | + items[j] = items[j - 1] |
| 17 | + items[j - 1] = tmp |
| 18 | + } |
19 | 19 | } |
| 20 | + } |
20 | 21 | } |
21 | 22 |
|
22 | | -//Implementation of bubbleSort |
| 23 | +// Implementation of bubbleSort |
23 | 24 |
|
24 | | -var ar=[5,6,7,8,1,2,12,14]; |
25 | | -//Array before Sort |
26 | | -console.log("-----before sorting-----") |
27 | | -console.log(ar); |
28 | | -bubbleSort(ar); |
29 | | -//Array after sort |
30 | | -console.log("-----after sorting-----") |
31 | | -console.log(ar); |
| 25 | +var ar = [5, 6, 7, 8, 1, 2, 12, 14] |
| 26 | +// Array before Sort |
| 27 | +console.log('-----before sorting-----') |
| 28 | +console.log(ar) |
| 29 | +bubbleSort(ar) |
| 30 | +// Array after sort |
| 31 | +console.log('-----after sorting-----') |
| 32 | +console.log(ar) |
32 | 33 |
|
33 | | -/*alternative implementation of bubble sort algorithm. |
| 34 | +/* alternative implementation of bubble sort algorithm. |
34 | 35 | Using a while loop instead. For educational purposses only |
35 | 36 | */ |
36 | 37 | /* |
37 | 38 | *In bubble sort, we keep iterating while something was swapped in |
38 | | -*the previous inner-loop iteration. By swapped I mean, in the |
39 | | -*inner loop iteration, we check each number if the number proceeding |
| 39 | +*the previous inner-loop iteration. By swapped I mean, in the |
| 40 | +*inner loop iteration, we check each number if the number proceeding |
40 | 41 | *it is greater than itself, if so we swap them. |
41 | 42 | */ |
42 | 43 |
|
43 | | -function bubbleSort(arr){ |
44 | | - let swapped = true; |
45 | | - while(swapped){ |
46 | | - swapped = false; |
47 | | - for(let i = 0; i < arr.length-1; i++){ |
48 | | - if(arr[i] > arr[i + 1]){ |
49 | | - let temp = arr[i]; |
50 | | - arr[i] = arr[i + 1]; |
51 | | - arr[i + 1] = temp; |
52 | | - swapped = true; |
53 | | - } |
54 | | - } |
| 44 | +function alternativeBubbleSort (arr) { |
| 45 | + let swapped = true |
| 46 | + while (swapped) { |
| 47 | + swapped = false |
| 48 | + for (let i = 0; i < arr.length - 1; i++) { |
| 49 | + if (arr[i] > arr[i + 1]) { |
| 50 | + const temp = arr[i] |
| 51 | + arr[i] = arr[i + 1] |
| 52 | + arr[i + 1] = temp |
| 53 | + swapped = true |
| 54 | + } |
55 | 55 | } |
56 | | - return arr; |
| 56 | + } |
| 57 | + return arr |
57 | 58 | } |
58 | 59 |
|
59 | | -//test |
60 | | -console.log("-----before sorting-----") |
61 | | -var array = [10,5,3,8,2,6,4,7,9,1]; |
62 | | -console.log(array); |
63 | | -console.log("-----after sorting-----") |
64 | | -console.log(bubbleSort(array)); |
65 | | - |
66 | | - |
| 60 | +// test |
| 61 | +console.log('-----before sorting-----') |
| 62 | +var array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1] |
| 63 | +console.log(array) |
| 64 | +console.log('-----after sorting-----') |
| 65 | +console.log(alternativeBubbleSort(array)) |
0 commit comments