Sorting Algorithms

Michael Kasingye
2 min readApr 26, 2021
Photo by Kelly Sikkema on Unsplash

Selection sort

The algorithm first iteration selects the smallest element in the array and swaps it with the first element. The second iteration selects the second smallest item and swaps it with the second element. This continues until the second-largest item is swapped with the second to last index leaving the largest element in the last index.

Insertion sort

The first iteration of this algorithm considers the second element in the array if it is less than the first element then swaps it. The second iteration looks at the third element and places it in the correct position considering the first two as the elements are sorted.

Merge sort

This sorts an array by splitting it into two equal sub-arrays, sorting each sub-array when merging it to one larger array.

Selection and insertion sorts are inefficient though consume less memory. At worse case, their run time is of the order of “n” squared due to re-iterations to find the elements to sort.

Merge sort is more efficient though consumes memory. At worse case, its run time is O(log n), as it further splits the array to sort them when merging hence fewer iterations.

Resources

Selection sort

Insertion sort

Merge sort

I hope this is useful.

Thank you

--

--

Michael Kasingye

I am a software developer. I love to build and work with teams to establish virtual platforms and systems that meet user satisfaction.