sorting algorithms

Many algorithms exist to sort data by numerical value. The two presented below are chosen because of their good all around use. Insertion sort is good for small arrays. Quick sort is good for large arrays. Many sorting libraries will actually create hybrid algorithms using insertion sort and quicksort concepts. But in the bigger picture it is useful to understand how some of the simpler algorithms work so that you can begin to think like a software engineer.

insertion sort

Insertion sort is the fastest of the sorting algorithms for arrays of size less than 10. It also has the advantage of performing very few operations on a nearly sorted array. The idea behind the insertion sort is to divide an array into a sorted part and an unsorted part. Begin with the first element of an array and call that the sorted array and then move unsorted elements into the correct position in the sorted array.

quick sort

Quick sort is known as being the fastest sort on average for arrays larger than 10 items. It should be noted that it performs poorly on sorted arrays especially if reverse sorted. The idea behind quick sort is each step you choose a pivot, generally at the end of your array, and move it into its correct sorted position. Then you partition the array into the portion of the array above the pivot and the portion below the pivot and begin the process again. The recursive calls end when all items have been pivoted into their correct position.