Address
Whiteland, IN 46184
Work Hours
Monday to Friday: 9AM - 5PM
Weekend: 1PM - 3PM
This is a continuation of Algorithms Sorting, in this write up we will introduce our last basic sorting algorithm which is the insertion sort.
Insertion Sort
The insertion sort builds up the sort by gradually creating a larger left portion which is always sorted.
Take the array for example
[ 5, 3, 4, 1, 2 ]
[ 3, 5, 4, 1, 2 ]
[ 3, 4, 5, 1, 2 ]
[ 1, 3, 4, 5, 2 ]
[ 1, 2, 3, 4, 5 ] //sorted array
Taking a look at the first array, insertion sort looks at the left or sorted portion of the array which is starting from 5, it compares 5 and 3 since 3 is less than 5 it inserts it before 5, now the sorted portion of the array is [3,5] It compares that to the next element which is 4 and places it where it belongs in the sorted portion of the array, this sorted portion then becomes [3,4,5]. The algorithm does this until it has come to the end of the array.
Pseudocode
Code For Insertion Sort
function insertionSort(arr){
var currentVal;
for(var i = 1; i < arr.length; i++){
currentVal = arr[i];
for(var j = i – 1; j >= 0 && arr[j] > currentVal; j–) {
arr[j+1] = arr[j]
}
arr[j+1] = currentVal;
}
return arr;
}
Intermediate Sorting algorithms
The algorithms in this category are a little more complex than the basic ones. They are also quite faster and can greatly improve performance depending on the situation. There are certain situations where a simple sorting algorithm will work better than an intermediate algorithm.
We will be looking at two sorting algorithms which are
Why do we need these sorting algorithms
Faster Sorts
Merge Sort
How does it work
Let us visualize it
[ 8, 3, 5, 4, 7, 6, 1, 2 ]
[ 8, 3, 5, 4 ] [ 7, 6, 1, 2 ]
[ 8, 3 ] [ 5, 4 ] [ 7, 6 ] [ 1, 2 ]
[ 8 ] [ 3 ] [ 5 ] [ 4 ] [ 7 ] [ 6 ] [ 1 ] [ 2 ]
[ 3, 8 ] [ 4, 5 ] [ 6, 7 ] [ 1, 2 ]
[ 3, 4, 5, 8 ] [ 1, 2, 6, 7 ]
[ 1, 2, 3, 4, 5, 6, 7, 8 ]
From the above layout, we see that the entire array was split until it had split all the elements into their individual array, then it began sorting them.
Pseudocode
Quick Sort
How does it work
[ 5, 2, 1, 8, 4, 7, 6, 3 ]
Let us pick a pivot 5, the algorithm takes all the elements less than 5 and puts them on the left side of 5 and all the elements that are greater than 5 and puts them on the right side of 5. Then we look at the left side of the array, pick a pivot point and do the same thing to it. We move the elements greater than it to the right and the elements greater than it to the left. We do this till we have single or 1 element arrays before we repeat the process on the right side.
5 → pivot
Pivot <–3 7→ pivot
1 4 6 8
2
Pivot Helper
Picking a Pivot
Pivot Pseudocode
QuickSort PseudoCode
QuickSort Code
function pivot(arr, start = 0, end = arr.length – 1) {
const swap = (arr, idx1, idx2) => {
[arr[idx1], arr[idx2]] = [arr[idx2], arr[idx1]];
};
// We are assuming the pivot is always the first element
let pivot = arr[start];
let swapIdx = start;
for (let i = start + 1; i <= end; i++) {
if (pivot > arr[i]) {
swapIdx++;
swap(arr, swapIdx, i);
}
}
// Swap the pivot from the start the swap point
swap(arr, start, swapIdx);
return swapIdx;
}
function quickSort(arr, left = 0, right = arr.length -1){
if(left < right){
let pivot index = pivot(arr, left, right) //3
//left
quickSort(arr,left,pivot index-1);
//right
quickSort(arr,pivotIndex+1,right);
}
return arr;
}
BigO complexity
Algorithm | Time Complexity(Best) | Time Complexity(Average) | Time Complexity(Worst) |
Insertion Sort | O(n) | O(n^2 ) | O(n ^2) |
Merge Sort | O(n log n) | O(n log n) | O(n ^2) |
Quick Sort | O(n log n) | O(n log n) | O(n log n) |