📘
snesjhon
  • You Should Know
  • Personal
    • Blogs
      • I'll never complain about web tooling again.
      • vscodevim
  • JavaScript
    • Closures
      • Closure Q1
    • Values
      • Primitive vs Reference
      • Accessing by value and reference
    • Function
      • call, apply, bind
      • Pass by value
      • Different types of Scopes
      • Context vs Scope
      • Parse Time Vs Run Time
    • Hosting
      • Are Let & Const Hoisted?
      • Hoisting Q1
      • Hoisting Q2
    • Standard Objects
      • Math
        • Math.log10
        • Math.pow()
    • Array
      • Apply
      • Slice vs Splice vs Split
    • This
      • This - Q1
    • TypeScript
      • as const
    • FAQs
      • Modulo Operator
      • Timeout
      • Declarative vs Imperative
      • ++i vs. i++
    • Interview Questions
  • react
    • FAQs
  • Ruby
    • Debugging
    • Symbols
    • Intro
  • Algorithms
    • Sliding Window
      • minSubArrayLen
      • maxSubArraySum
      • findLongestSubstring
    • Frequency Counter
      • sameFrequency
    • Recursion
      • nestedEvenSum
      • flatten
      • Reverse a String
      • Fibonnacci
    • Searching
      • overlappingIntervals
      • twoStringSearch
      • binarySearch
    • Sort - Elementary
      • Selection Sort
      • Bubble Sort
      • Insertion Sort
      • quickSort
    • Sort - Intermediate
      • Radix Sort
      • Merge Sort
    • FAQs
  • Data Structures
    • Breadth First Search
    • Linked Lists
      • Singly Linked Lists
    • FAQs
  • Code Challenges
    • LeetCode
      • removeDuplicates
    • Hacker Rank
      • twoSums
      • Sock Merchant
    • Hacker Rank - Medium
      • New Year Chaos
  • Databases
    • SQL
Powered by GitBook
On this page
  1. Algorithms
  2. Sort - Elementary

Bubble Sort

Sort an array using bubble sort

bubbleSort([4,3,2,1]) // [1,2,3,4]

This is the brute force way. Two for loops that continuously swap regardless of length

function bubbleSort(){
    for(var i = 0; i < arr.length; i++){
        for(var j = 0; j < arr.length; j++){
            if(arr[j] > arr[j + 1]){
                var temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

This is a very naive way of sorting because we're having to continously loop through the array multiple times. The i goes through the whole array and continues until the end of the array. j goes through the array and also continues until the end of the array but we compare j and `j

function bubbleSort(arr){
 // first we create a base condition
 if(arr.length < 1) return arr; 

 // create the first loop at the end of the array that decrements
 // This "starts" at the end of the loop based on it's arr.length. 
 // it's arr.length instead of `arr.length` because we're not comparing equality
 // but rather if it's less than (not less than or equal)
 for(var i = arr.length; i > 0; i--){
  // create a second loop that the start of the array that increments 
  // until we react the position of i
  for(var j = 0; j < i; j++){
   // Let's compare the value of `j` and `j + 1` to get a lookahead
   // This will go until i 
   if(arr[j] >= arr[j+1]){
    // swap the values if true
    var temp = arr[j];
    arr[j] = arr[j + 1];
    arr[j + 1] = temp;
   }
  }
 }
 // return the sorted array;
 return arr;
}

Last updated 4 years ago