バブルソート( JSの例)


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

バブルソート概要

  • 最悪の複雑さ
  • 平均複雑度
  • 最良の複雑さ
  • スペース複雑さ:1
  • 交換方法
  • 安定した
  • 比較ソート
  • バブルソートノート


    バブルソートは、時折沈没ソートと呼ばれる、単純に並べ替えアルゴリズムは、繰り返しリストを介して、隣接する要素を比較し、スワップをしている場合は、間違った順序にある場合.リストをソートするまでリストを通過します.

    バブルソート


    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
    

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



    My FAANG interview study notes
    Bubble Sort Github
    Clean Code