gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
Is insertion sort a stable sorting algorithem
- User
Is insertion sort a stable sorting algorithem
- Assistant
Yes, insertion sort is a stable sorting algorithm.
Stability in sorting algorithms means that if two elements have the same key (or value), their relative order is preserved in the sorted output as it was in the input.
Insertion sort works by building the sorted array one element at a time, inserting each new element into its proper place relative to the already sorted elements. Because it inserts elements by shifting larger elements to the right and placing the current element in the first position where it is smaller or equal, it does not change the order of equal elements. This preserves stability.
Summary:
- Insertion sort is stable
- It keeps equal elements in the same relative order
- This is useful when stability is required, such as sorting records by multiple criteria.