-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathbubbleSort.js
More file actions
35 lines (27 loc) · 1 KB
/
Copy pathbubbleSort.js
File metadata and controls
35 lines (27 loc) · 1 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
// Bubble Sort Algorithm in JavaScript
function bubbleSort(arr) {
// 'n' is the length of the array
let n = arr.length;
// Outer loop for each pass
for (let i = 0; i < n - 1; i++) {
// Inner loop for comparing adjacent elements
for (let j = 0; j < n - i - 1; j++) {
// Compare two adjacent values
if (arr[j] > arr[j + 1]) {
// Swap them if they are in the wrong order
let temp = arr[j]; // store arr[j] in temp
arr[j] = arr[j + 1]; // move smaller element to left
arr[j + 1] = temp; // move larger element to right
}
}
// You can print the array after each pass to see progress
console.log(`After pass ${i + 1}:`, arr);
}
// Return the sorted array
return arr;
}
// Example usage:
let numbers = [5, 3, 8, 4, 2];
console.log("Original array:", numbers);
let sortedArray = bubbleSort(numbers);
console.log("Sorted array:", sortedArray);