Insertion Sort Algorithm in PHP

Insertion sort algorithm was developed using comparison algorithm. This algorithm splits the array into two sub-arrays namely sorted and un-sorted sub arrays. Initially the sorted sub array contains only one element in other words the first item of a main array was considered as a sorted array and the rest of the items was considered as a un-sorted array. Take a leftmost element in a un-sorted array to find the suitable place in the sorted array and finally place the number in a suitable place holder till there is no elements exist in the un-sorted sub array. Insertion sort is an easiest way of sorting technique, but it's not an efficient on large data sets. Moreover, Insertion sort is more efficient and advanced algorithm than the Quick sort, Heap sort or merge sort.

Time Complexity

  • Best complexity: n
  • Average complexity: n^2
  • Worst complexity: n^2