Bubble Sort, Selection Sort, and Insertion Sort with JavaScript

the joy of sorting
JavaScript .sort() without argument
JavaScript .sort() with comparator function

Bubble Sort

Bubble sort animation
  • declare a function that takes in an array
  • iterate through every single element of the array to compare with the element at the next index
  • if the current element is bigger than the next element, swap their locations
  • have a limit of the upper bound of the iteration so sorted element wouldn’t be compared again
  • return the sorted array
bubble sort code sample

Selection Sort

selection sort animation
  • declare a function that takes in an unsorted array
  • iterate through the data from the start of the array and declare a variable to keep track of the index of the smallest valued element in the unsorted data
  • start an inner loop to compare values to find the smallest values then swap into the right position
  • repeat until sorted and return the array

Insertion Sort

insertion sort animation
  • declare a variable for tracking current value starting with the second value of the array
  • compare that value to the values that comes before it to find the right position in the sorted portion of the array
  • repeat the process until array is sorted
insertion sort sample code

--

--

--

Software Engineer, React.js || Javascript || RoR

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Do You Need to Break Up With Your Tech Vendor?

Azure Functions — Event hub processing

Pros and Cons of Cloud Computing.

How to create and connect SQL server to Visual Studio?

Rock Paper Scissors in Python

Using AWS Lambda environment variables from local .env file (Python 3.7)

Three Ways to Keep Project Constraints from Dooming Your Project

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pan Li

Pan Li

Software Engineer, React.js || Javascript || RoR

More from Medium

Why Digimon has Surpassed Pokémon

BINARY SEARCH TREES: DELETION

ES2021(ES12) ships with below new features

Ace of Spades — Asymmetrical Play Patterns