# Bubble Sort, Selection Sort, and Insertion Sort with JavaScript

Sorting values in an array is probably the most frequent task a programming beginner like myself encounters. If you’re familiar with JavaScript, you must’ve heard about the .sort() function that JavaScript is shipped with behaving a certain way when it comes to sorting numeric values. It sorts all values by converting them to strings and comparing strings in UTF-16 code units order. The result is not as favorable as what humans perceived as “sorting numbers”.

The good news is, we’re able to pass in a comparator function in the argument to make it the way we want it to behave whether to sort the numbers in ascending order or descending.

This is all handy-dandy but what is possibly happening under the hood to make this magic happen? Who is that helpful wizard behind the curtain who helps us to put things in the right place? Let’s explore some of the simple sorting algorithms, bubble sort, selection sort, insertion sort, and dive deep into analyzing their efficiencies.

**Bubble Sort**

sometimes referred to as **sinking sort**, it repeatedly compares values from adjacent elements to ensure they’re in the right position. When the process is visualized, you will see elements with bigger values bubble up to the end of the array or smaller values sink to the front of the array, depending on your code implementation, therefore its name. The algorithm divides the input list into two parts, a sorted sublist toward the end of the array and the rest of the unsorted elements before it.

Here are the steps for implementation with the bubbling approach:

- 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

It’s not the most efficient nor the most common sorting algorithm since it takes the same amount of iteration as the input size if not more to sort an array. However, with the implementation of the noSwap variable in the code example, it can actually be very effective in sorting a nearly sorted array. The average time complexity of this sort is O(n²), which means that its efficiency decreases dramatically as the size of the input grows.

# Selection Sort

Similar to the logic of bubble sort, it also divides the input into two sublists of data. Instead of constantly comparing values and swapping locations of two elements, selection sort selects the index of the smallest value in the array by comparing values in the unsorted portion of the input. Then place the element with the lowest value to its right position after looping through the entire input set.

Here are the steps if I were to go about the implementation:

- 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

and your code may look like this,

You might think selection sort should be more efficient than bubble sort since we’re only keeping track of an integer representing an index instead of constantly mutating the input data. The truth is we’re still visiting each element multiple times to find the right place for each element. Its time complexity is still a sad O(n²) at average data size.

# Insertion Sort

Just like its siblings, bubble sort and selection sort, insertion sort also build around the concept of having two sublists of data. During the iteration, insertion sort takes out one element from the input data, finds the location it belongs within the sorted sublist, and inserts it there, shifting all elements bigger value up by one index to maintain the input size integrity. It repeats until no input elements are untouched.

Here’s the pseudocode I’m going about implementing this:

- 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

Although insertion sort also has the time complexity of O(n²), it’s well perceived as the more efficient algorithm among the quadratic sorting algorithms.

I hope you’re feeling a bit more familiar with these three simple sorting algorithms as I do now. I’m looking forward to exploring the more advance sorting method and sharing my experience with you soon.