Bubble Sort (JS Example)

Clean Code Studio - Jul 13 '21 - - Dev Community

Twitter Follow

Note: This is not a "professionally written" post. This is a post sharing personal notes I wrote down while preparing for FAANG interviews.

See all of my Google, Amazon, & Facebook interview study notes

Bubble Sort Overview

  • Worst Complexity: n^2
  • Average Complexity: n^2
  • Best Complexity: n
  • Space Complexity: 1
  • Method: Exchanging
  • Stable: Yes
  • Class: Comparison Sort

Bubble Sort Notes

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

Bubble Sort JS Implementation

const BubbleSort = (items = []) => {
  for (let i = 0; i < items.length; i++)
  {
    for (let j = 0; j < items.length; j++)
    {
      if (items[j] > items[j + 1])
      {
         let temporary = items[j]
         items[j] = items[j + 1]
         items[j + 1] = temporary
      }
    }
  }

  return items
}


module.exports = BubbleSort
Enter fullscreen mode Exit fullscreen mode

FAANG Study Resource: Cracking the Coding Interview
(Google Recommended)


My FAANG interview study notes

Bubble Sort Github

Clean Code

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player