Gist / July 2, 2023 / 1 min read / By Mahi Garg

Insertion Sort : Swift

Insertion sort is a simple sorting algorithm in Swift that builds the final sorted array one element at a time. It iterates through the array, comparing each element with the sorted portion and placing it in the correct position. This process continues until all elements are sorted. Insertion sort has a time complexity of O(n^2) in the worst case, but it performs well on small data sets or partially sorted arrays. It also has an advantage in that it can efficiently sort elements in real-time as they are received or generated.