If you've come this far, thanks so much for reading! I hope this was a helpful tutorial to anyone learning about sorting algorithms, JavaScript, or programming fundamentals in general.Now, sorting different commodities with defects of specific size is made easy. I recommend looking at this handy visualization again to help lock in the logic: We repeat the while loop, iterating through the array multiple times until it's completely sorted, and then return the sorted array.If a value is greater than its neighbor to the right, we swap the two and proceed (and set isSorted to false.) In our for loop, we iterate through the entire array and compare values.Every time the while loop begins, isSorted is set to true, so that if no swaps are performed in the for loop the while loop will end. Our while loop runs perpetually until isSorted is equal to true, in which case it stops.Let's walk through the logic one more time. The final JavaScript code will look like this:Įnter fullscreen mode Exit fullscreen mode Now that I've successfully sold you on Bubble Sort (or made you want to steer clear of it forever), let's get down to implementing it in code! However, this could be considered an edge "best case" rather than a norm, and while other algorithms might take longer to check for an already sorted array, the overall inefficiency of Bubble Sort still loses out.īubble Sort has a worst case and average case runtime complexity of O(n^2), and a space complexity of O(n). One advantage that Bubble Sort has over other sorting algorithms is that its core logic has a built-in check to see if an array is already sorted, resulting in an O(n) runtime if a sorted array is passed in, since only one iteration through the array will be required. Other sorting algorithms like Quick Sort, Heap Sort, or Merge Sort should always be used instead for most practical purposes. As I mentioned before, Bubble Sort is notoriously slow and inefficient, relegating it to being mostly used as an educational tool rather than a practical one. Unfortunately, "getting the job done" isn't the only requirement for a sorting algorithm. Here's a helpful visual representation of what happens while the algorithm is running:Īs you can see, each iteration swaps greater values to the right multiple times until the greatest value in the array is found, which will then be swapped all the way to the end. This essentially "bubbles up" the greatest value to the end of the array each time the iterative loop runs, slowly but surely putting the values in their proper sorted positions. Unfortunately it's also one of the least efficient, and is almost never used in practical programming applications.Įssentially, the algorithm iterates over an array multiple times (or once, in the edge case of an array already being sorted), comparing each element to the element to the right of it and swapping them so that the greater element is to the right. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list.įrom the standpoint of an educational tool, Bubble Sort is actually one of the simplest sorting algorithms to comprehend and implement. The pass through the list is repeated until the list is sorted. The Wikipedia page on Bubble Sort describes the algorithm as follows:īubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. Today, we'll be looking at Bubble Sort, another one of the main sorting algorithms in Computer Science. While it may not come up often, there's always the chance you may be asked to implement or explain a sorting algorithm in a technical interview setting, which is exactly what this post is here to prepare you for! While using a language's built-in sorting functions may get the job done for most day-to-day work, it's important to understand what's going on under the hood, and what different sorting algorithms are actually doing and why they work the way they do. We rely on them every day as programmers and engineers to sift through data, and they're built into nearly every modern programming language in one way or another. In computer science, few tools are used quite as often as sorting algorithms. Welcome to the third entry in my Sorting Algorithms in JS series here on Dev! I've previously covered both Selection Sort and Insertion Sort in previous posts, so check those out if you'd like to learn more about sorting algorithms in JS.
0 Comments
Leave a Reply. |