mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00

* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
/* Bubble Sort is an algorithm to sort an array. It
|
|
* compares adjacent element and swaps their position
|
|
* The big O on bubble sort in worst and best case is O(N^2).
|
|
* Not efficient.
|
|
* Somehow if the array is sorted or nearly sorted then we can optimize bubble sort by adding a flag.
|
|
*
|
|
* In bubble sort, we keep iterating while something was swapped in
|
|
* the previous inner-loop iteration. By swapped I mean, in the
|
|
* inner loop iteration, we check each number if the number proceeding
|
|
* it is greater than itself, if so we swap them.
|
|
*
|
|
* Wikipedia: https://en.wikipedia.org/wiki/Bubble_sort
|
|
* Animated Visual: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
|
|
*/
|
|
|
|
/**
|
|
* Using 2 for loops.
|
|
*/
|
|
export function bubbleSort(items) {
|
|
const length = items.length
|
|
let noSwaps
|
|
|
|
for (let i = length; i > 0; i--) {
|
|
// flag for optimization
|
|
noSwaps = true
|
|
// Number of passes
|
|
for (let j = 0; j < i - 1; j++) {
|
|
// Compare the adjacent positions
|
|
if (items[j] > items[j + 1]) {
|
|
// Swap the numbers
|
|
;[items[j], items[j + 1]] = [items[j + 1], items[j]]
|
|
noSwaps = false
|
|
}
|
|
}
|
|
if (noSwaps) {
|
|
break
|
|
}
|
|
}
|
|
|
|
return items
|
|
}
|
|
|
|
/**
|
|
* Using a while loop and a for loop.
|
|
*/
|
|
export function alternativeBubbleSort(arr) {
|
|
let swapped = true
|
|
|
|
while (swapped) {
|
|
swapped = false
|
|
for (let i = 0; i < arr.length - 1; i++) {
|
|
if (arr[i] > arr[i + 1]) {
|
|
;[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]
|
|
swapped = true
|
|
}
|
|
}
|
|
}
|
|
|
|
return arr
|
|
}
|