Insertion sort

This is a non-optimal straightforward sorting algorithm for typical uses case but can get linear time for nearly-sorted lists. The algorithm works by keeping track of the unsorted and the sorted portions and inserting an unsorted element in the correct spot of the sorted list each time.

Pros Cons
Stable, in-place, and online algorithm Quadratic worst case time complexity
Linear best-case performance for nearly-sorted lists
More efficient (factor-wise) than other quadratic comparison algorithms
LeetCode Problems
Merge Intervals
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Performance
Worst-case O(n^2)
Average O(n^2)
Best-case O(n)
Worst-case space O(n)

Similar