Before you go, check out these stories!

Hackernoon logoAlgorithms for Beginners: Bubble Sort in JavaScript by@Swordfish

Algorithms for Beginners: Bubble Sort in JavaScript

Author profile picture


JS, React, Redux, Ruby, Rails, SQL

Algorithms are a fundamental part of software and coding. Algorithm is this fun buzzword that makes something sound really complicated and cool. I’d like to point out that an “algorithm” literally is just a way of doing something; it’s just a process. Nonetheless Algorithms and Data-Structures are a core part of software because at the end of the day you are just working with data. Data needs to be organized for it to be meaningful just like the letters on this page. Atwh and whAt have the same letters but the latter has meaning because of the organization.

When we talk about “Algorithms” in software and coding we really just mean a specific way to organize specific data. One of the best parts of working in software is that there is no one “correct” way to do something. If it works it works. However there are ways that things can be optimized. Here is a resource on how to optimize for time complexity and space complexity. All that means is we need to account for how much memory and how much time something takes. One of the easiest Algorithms, which isn’t used in “production”, is Bubble sort.

Bubble sort has a lot of time complexity. It’s BigO² or O(n²) meaning it takes time. For a small snippet this is not really noticeable. However if you are professionally building something that same calculation or logic could take minutes, even tens of minutes as the data weight increases. Bubble Sort is useful to help you get started on learning Algorithms. As a beginner think about the problem:

“How to order an array of numbers smallest to largest?”

Because we are beginning we are going to use the Brute Force technique which ironically is what bubble sort exactly does. In general terms lets start a “for loop” and iterate over the array and look at each element compared to the next element (

element +1
) at a time; (element > element + 1). If this statement returns true then you will swap them. Again using brute force how would you do that? I’m sure you can think of ways to do this.

Then the solution is totally simple.

We just assign

array[i] = array[i+1]
. To assign
to the value of
you must create temporary storage slot.

I’ll explain why is a few sentences why.

Let’s walk through what is happening line by line:

We have a function named bubble after the “bubble sort” algorithm we are about to use. A variable named recursion set to false. We use the term because the function will call itself until it is sorted. If we have to preform a swap of any of the elements in the array we will reset recursion to true because it will have to call itself again. If a swap happens then the array is not sorted yet. On line 5 we have an “if” condition.

If the element before the next element is bigger then we set recursion to true and then the swap procedure is preformed. Then we create a variable and call it “storage”. Storage will hold the value that is being moved. We do this because we are about to overwrite it. Next on line 9 we set the element with the lower value to the the lower slot in the array(array[i]; we reassign the element. Now the higher element is overwritten to the lower element value.

With assignment; if you know your precedence of operations ; you can see what is happening. We see the = operator which will run right to left; what is on the left of = is then assigned to what is on the right. The value on the right of the "=" operator will be assigned to the variable on the left. With an array of

on the first pass, storage will be
, and here on line 9 we set
(this is the
). Because storage is now
we can use it to replace
with it. We can no longer use
because at this point it is now

Bubble sort swaps values adjacent to each other. We need to put 5 where

is right now. In this instant of time or at this “tangent” of values
is both at
. Here JavaScript is annually using “Pass by Value”. We know this because “storage” was set to
. This reference or
is currently
. The number
is a primitive datatype and thus JavaScript uses a pass by value here.

No matter what

is storage will always be
at this point; unless it is reassigned. JavaScript has assigned storage to the “address” in memory that is allocated to the primitive datatype
. We thus use the storage variable to assign
to storage;
is now
. The array at this point in time looks like
[3,5, 1]

Terminal/Console for bubble.js

You can see in the js file above there are console.logs of what the code is doing. I put in the logic gate of (

array[i] > array[i+1]
) so you can see when this returns false and does not run.

At the end this is always false and thus recursion is never assigned to ‘ture’. Bubble sort then returns the array.


Become a Hackolyte

Level up your reading game by joining Hacker Noon now!