Understanding sorting algorithms can be very useful, if you want to grow as a programmer. Here we are going through the most common sorting algorithms and explain how they work.

Bubble sort is one of the slowest, but also simplest sorting algorithms out there.
line 1: we loop through the array.length - 1, because we won't be comparing the last element.line 2: we loop through the array.length - 1 - j, because we need (n-2) iterations at the 2nd pass and (n-3) at the third pass.. and so on line 3: if current member is bigger than element to the right, we swap them |

quick sort sorts the element by pushing the el. to either **left** or **right** array if the mebmber is less than / bigger than or equal the first member (pivot point)

use recursive call in return to merge the 3 arrays together

use recursive call in return to merge the 3 arrays together

Insertion sort should be used to sort short rather than long arrays

**line 1: **we get** **our value at specified index

**line 2: **we set **j** to be less than** i **than enter our for loop and see 2 conditions: if **j** is 0 or positive and member to the left (**j**) of **temp **member is bigger than **temp ** element, copy every higher element on index ahead

5, 9, 13, 4, 1, 6 // the only sorted part is the first item

5, 9, 13, 4, 1, 6 // 9 > 5 so we don't move it

5, 9, 13, 4, 1, 6 // 13 > 9 we don't move it

4, 5, 9, 13, 1, 6 // compare all to 4, until we reach the head

1, 4, 5, 9, 13, 6 // compare all to 1, until we reach the head

1, 4, 5, 6, 9, 13 // first smaller item is 5, place 6 before it

5, 9, 13, 4, 1, 6 // 9 > 5 so we don't move it

5, 9, 13, 4, 1, 6 // 13 > 9 we don't move it

4, 5, 9, 13, 1, 6 // compare all to 4, until we reach the head

1, 4, 5, 9, 13, 6 // compare all to 1, until we reach the head

1, 4, 5, 6, 9, 13 // first smaller item is 5, place 6 before it

selection sort - worse on large lists

heap sort

merge sort

Source: https://hackernoon.com/programming-with-js-insertion-sort-1316df8354f5

heap sort

merge sort

Source: https://hackernoon.com/programming-with-js-insertion-sort-1316df8354f5

Powered by
Create your own unique website with customizable templates.