## Building Scalable Artificial Intelligence Products with TensorFlow.js

Explore the power of TensorFlow.js; an open-source library that enables developers to define, train, and deploy machine learning models entirely in the browser and on Node.js.

Welcome everyone to a new story on devspedia. Today I'll showcase 8 sorting algorithms in JavaScript, with examples and complexity comparisons.

Let's get straight to it. Below is a list of these sorting techniques:

- Bubble Sort
- Selection Sort
- Insertion Sort
- Heap Sort
- Merge Sort
- Quicksort
- Shellsort
- Radix Sort

Bubble sort works basically by iterating through the list while comparing list each adjacent pair of items in the list. If the order of these 2 items is invalid (based on whether you're sorting ascending or descending) then it swaps them.

It keeps repeating this same pattern over and over again until no extra swapping is necessary.

**Complexity:**

- Best case:
`n`

- Worst:
`n`

^{2} - Average:
`n`

^{2}

Selection sort is has significant time complexity `O(`

which makes it very slow for large lists. Selection sort works by doing in-place comparison, so it'll iterate through all items and compare them 1 to 1, then start over. It keeps repeating again no more replacing is necessary.*n*^{2})

Selection sort is similar to insertion sort, however it performs worse. But in some scenarios selection sort can perform well in low memory systems, and with smaller lists.

**Complexity:**

- Best:
`n`

^{2} - Worst:
`n`

^{2} - Average:
`n`

^{2}

Insertion sort is — similar to previous selection sort — takes on item at a time too, it compares one item with it takes first item to the left, and compare it with the next item, then second item, and put before or after first item, then third item and compare it with second then first item and keep repeating this until all list is sorted.

**Complexity:**

- Best:
`n`

- Worst:
`n`

^{2} - Average:
`n`

^{2}

Heap sort performs also runs on comparisons, but it's more efficient than selection or insertion sort because with every iteration it shrinks down the sortable scope of the list, resulting in fewer iterations until list is fully sorted.

It works by creating a max (or min) heap, and then take the top item of the heap tree and put it in the far right (if max heap) or far left (if min heap), and consider that number sorted, and then work only on the remaining items.

**Complexity:**

- Best:
`n log(n)`

- Worst:
`n log(n)`

- Average:
`n log(n)`

Merge sort is a divide and conquer algorithm. Merge sort works by splitting the list into halves, and further until each group is only at most 2 items. Then it keep merging and growing up again to the full length of the list.

The steps for sorting will be by splitting them into smaller groups, compare groups together, then merge sorted items to a bigger group, then compare, then merge, and keep repeating until all items are sorted.

**Complexity:**

- Best:
`n log(n)`

- Worst:
`n log(n)`

- Average:
`n log(n)`

Quicksort is also a divide and conquer algorithm. It works by dividing the list into two smaller groups: group with lowest elements, and group with highest elements. It knows the lowest and highest elements by picking an item from the middle off the list, and grouping smaller values in 1 group, and higher values in another. Then quicksort can then recursively sort the the smaller groups by repeating the same steps.

**Complexity:**

- Best:
`n log(n)`

- Worst:
`n`

^{2} - Average:
`n log(n)`

Quick sort is another in-place comparison sort algorithm. It works by sorting pairs of elements far apart from each other using an interval value for this distance that decreases while iterating, then progressively reducing the gap between elements to be compared. Starting with far apart elements, it can move some out-of-place elements into position faster than a simple nearest neighbor swap.

Check the following graphs for better explanation:

Let us consider the following example to have an idea of how shell sort works. We take the same array we have used in our previous examples. For our example and ease of understanding, we take the interval of 4. Make a virtual sub-list of all values located at the interval of 4 positions. Here these values are {(35,14)}, {(33, 19)}, {(42, 27)} and {(10, 44)}

We compare values in each sub-list and swap them (if necessary) in the original array. After this step, the new array should look like this:

Then, we take interval of 1 and this gap generates two sub-lists - {(14, 27, 35, 42)}, {(19, 10, 33, 44)}

We compare and swap the values, if required, in the original array. After this step, the array should look like this:

Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sort to sort the array.

Following is the step-by-step depiction:

**Complexity:**

- Best:
`n log(n)`

- Worst:
`n (log(n))2`

- Average: Depends on gap sequence

Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. A positional notation is required, but because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers.

In mathematical numeral systems, the radix or base is the number of unique digits, including the digit zero, used to represent numbers in a positional numeral system. For example, a binary system (using numbers 0 and 1) has a radix of 2 and a decimal system (using numbers 0 to 9) has a radix of 10.

**Complexity: (k means the longest key)**

- Best:
`n * k`

- Worst:
`n * k`

- Average:
`n * k`

**Resources:**

- https://en.wikipedia.org/wiki/Bubble_sort
- https://en.wikipedia.org/wiki/Selection_sort
- https://en.wikipedia.org/wiki/Insertion_sort
- https://en.wikipedia.org/wiki/Heapsort
- https://en.wikipedia.org/wiki/Merge_sort
- https://en.wikipedia.org/wiki/Shellsort
- https://www.tutorialspoint.com/data_structures_algorithms/shell_sort_algorithm.htm
- https://en.wikipedia.org/wiki/Radix_sort
- https://www.researchgate.net/figure/Simplistic-illustration-of-the-steps-performed-in-a-radix-sort-In-this-example-the_fig1_291086231
- https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting

Thanks for reading devspedia, I love you, and see you the next time :)