Lua - Insertion Sort



Insertion Sort builds the sorted array by inserting an element into the sorted part of the array one by one. It iterates through each elements of the array and then insert the element into the sorted part of the array.

main.lua

-- function to sort a list using insertion sort
function insertion_sort(list)
   -- length of the list
   local n = #list 
   -- start iterating from 2nd element   
   -- 1st element is considered sorted being only element
   for i = 2, n do 
      -- get the current key to be inserted
      local key = list[i] 
      -- index of last element of sorted list
      local j = i - 1 

      -- move numbers greater than key towards right
      while j > 0 and list[j] > key do
         list[j + 1] = list[j] 
         j = j - 1 -- move to previous element in sorted list
      end
      -- insert key in its sorted position
      list[j + 1] = key
   end
   -- list is sorted, return it
   return list 
end

-- Example usage:
local numbers = {5, 1, 4, 2, 8}
local sorted = insertion_sort(numbers)
print("Sorted list:", table.concat(sorted, ", "))

Output

When we run the above program, we will get the following output−

Sorted list:	1, 2, 4, 5, 8

Working of Insertion Sort

  • Iterate through each element − We've started loop from 2nd element considering the 1st element is sorted being solo element of the sorted part of the list.

  • Pick Key − Current element being iterated is considered as key to be inserted.

  • Comparison and Shifting − We're comparing key with each element of the sorted part of the list. If element in sorted part is greater than key, then element is shifted to right and makes space for key to insert.

  • Insertion − The shifting process continues till key is greater than element or start of the sorted part is reached. Then key is inserted in its sorted position.

  • Building the Sorted List − The above steps are repeated for remaining unsorted list gradually building the sorted part of the list from left to right.

Time Complexity

  • Worst Case and Average Case − O(n2), where n is number of elements. We may need to perform multiple iterations through all elements of a list in case list is reversely sorted. In such a case, we have to compare each element and shift it past all elements of the sorted part of the list.

  • Best Case O(n) − O(n) when list is sorted as inner while loop is not executed and each element is already in its correct position.

Space Complexity

  • O(1) − Space complexity of insertion sort is constant as it is not requiring any extra memory from the list.

When to use Insertion Sort

Insertion sort is simple and easy to learn and is used in following areas −

  • Small Set of Data − In case of smaller set of data, insertion sort performed well due to low overhead.

  • Nearly Sorted Data − When a list is nearly sorted, the time complexity of Insertion sort is quite low, as minimal shifting is needed.

  • Online Sorting − In case, elements of a list are appearing one at a time, insertion sort is quite useful to insert the element into the sorted list as they arrive.

Advertisements