Sorting Algorithms

Sorting Algorithms hackernoon.com4 years ago in#Dev Love28

Originally published by Jed Record on August 21st 2017 Gryffindor! When you hear “sorting algorithm.” What comes to mind? Quick sort? Merge sort? Counting sort? Bubble sort? The sorting hat from Harry Potter and its algorithm to place students in a house? All jokes aside, I think it’s important to recognize these algorithms because well, where would we be without sorting? After all, in order to employ Binary Search, we must have our elements sorted. How would we look up words in a dictionary, or names in a phonebook? A lot of our day to day mundane tasks are a lot simpler because of sorting. By sorting, we mean to put elements in a list in a certain order. That could be in numerical order or lexicographical order(alphabetical order). Even though most programming languages include some kind of sort function out of the box, it’s good to take a step back and pick them apart so we actually know what’s going on behind the scenes. In this week’s blog, we will explore what’s happening under the hood with various sorting algorithms in JavaScript. We will start with less performant algorithms and work our way to more performant algorithms. Bubble Sort Time Complexity — Best Case: Ω(n)Average Case: Θ(n²)Worst Case: O(n²) Space Complexity — Worst Case: O(1) Back in 2007 during his campaign run, Obama had visited Google and had this little exchange with then CEO, Eric Schmidt during a Q&A session. Eric Schmidt: What is the most efficient way to sort a million 32-bit integers?President Obama: Well…Eric Schmidt: Maybe — I’m sorry…President Obama: No, no, no, no. I think — I think the bubble sort would be the wrong way to go.Eric Schmidt: [facepalm] Come on. Who told him this? Okay. I didn’t see computer science in your background.President Obama: We’ve got our spies in there. Bubble sort is not the greatest, apparently even Obama was briefed on this before the interview. The reason it’s not is because we have many more efficient alternative solutions to handle sorting now. Let’s go to the code! two implementations of bubble sort If you are not familiar with bubble sort at all or have forgotten how it works, I recommend searching for a visualization of it online. Back when I learned about sorting, visualizing the steps helped my comprehension of them. The way bubble sort works is that for each element we iterate through, we want to check to see if the next element in index is less than it. If it is, we will essentially swap the elements. In both implementations you can see that I am using a temp variable just as a placeholder so that I can properly switch the elements in the array. In the second implementation I am using a while loop that runs while swapped is true. At the beginning of an iteration we immediately set swapped to false. If we detect an element that requires a swap, we set swapped to true and continue swapping elements. Selection Sort Time Complexity — Best Case: Ω(n²)Average Case: Θ(n²)Worst Case: O(n²) Space Complexity — Worst Case: O(1) Selection sort is known for its simplicity.  » Read More

Like to keep reading?

This article first appeared on If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply