DEV Community

Liz P
Liz P

Posted on

Sorting Algorithms (Part 1)

I talked about algorithms last time here and concluded that there is just so much information and so many resources surrounding algorithms and algorithm practice. It can be a bit of an overload. So this time I wanted to discuss something very specific - some common easy sorting algorithms. Because in JavaScript we need to sort things a lot, right?

Bubble Sort
Insertion Sort

Bubble Sort - Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and precise.

So the simplest explanation here is bubble sort takes an unsorted array and compare two elements at a time and swaps the elements if they aren’t already in order. And continues down the line until your array is sorted.

Image description

So with the above, you start by comparing 14 and 33, and these two elements are already in their proper order. Then you move on to comparing 33 and 27, because 33 is greater than 27 these two get swapped. Now you’ve got 14, 27, 33 which takes you to comparing 33 and 35, which are in the correct order so you move to compare 35 and 10 and because 10 is less than 35 those get swapped. So after the first iteration you’ve got 14, 27, 33, 10, 35. You start over again and keep going until 10 is in it’s proper place at the beginning.

**Insertion Sort - Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.

The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2), where n is the number of items.

Image description

Again, using an unsorted array as an example here we compare the first two elements which are already in ascending order and 14 is in the sub-array which is sorted. Moving on to compare 33 and 27 which aren’t in ascending order so they’re swapped and it checks against the sub-array which 14 is less than 27 so that’s fine. So now 14 and 27 are in our sub-array. Moving on to compare 33 and 10, those get swapped. Then compare to the sub-array and 27 and 10 then get swapped, then compare 14 and 10 and swap again. So now our sub-array is a sorted array of 10, 14, 27 and this continues on until the entire array has been compared and you’re left with the sorted sub-array.

Both of these algos are fairly easy to understand. Next time I’m going to cover a few more sorting algorithms in increasing difficulty. To see more visuals or go more in-depth on these I recommend reading up more on them or even taking a course that covers algorithms. Colt Steele’s Data Structures and Algorithms course covers all the sorting algos and he does a deep dive into each type of sorting algorithm and takes you through some code examples of each. Thanks for reading!

Top comments (0)