Sub topic 9
Stability, In-Place Sorting, and Adaptivity
Assume you know arrays, loops, recursion, and basic time complexity. This chapter focuses on three properties that help you choose between sorting algorithms: stability, in-place behavior, and adaptivity.
Stability
A sorting algorithm is stable if records with equal keys keep their original relative order. A key is the field used for comparison.
Suppose records are initially ordered as follows:
| Original position | Name | Marks |
|---|---|---|
| 1 | Ana | 80 |
| 2 | Ben | 70 |
| 3 | Cara | 80 |
A stable sort by marks produces Ben, Ana, Cara. Ana remains before Cara because both have the key 80.
Stability matters when sorting records by multiple fields. For example, you can first sort by name and then stably sort by marks. The second sort preserves name order among students with equal marks.
Insertion sort, bubble sort, and merge sort can be stable with suitable implementations. Selection sort and ordinary in-place quick sort are usually not stable.
In-Place Sorting
A sorting algorithm is in-place when it uses only constant extra storage, usually written as auxiliary space \(O(1)\). It may modify the input array by swapping or shifting elements.
“In-place” does not mean that the algorithm uses no memory at all. Variables, loop counters, and a small number of temporary values are still allowed. Recursive algorithms also use call-stack space, so their total extra space may exceed \(O(1)\).
Insertion sort is both in-place and stable:
def stable_insertion_sort(records):
for i in range(1, len(records)):
item = records[i]
j = i - 1
# Shift only larger keys, so equal keys keep their order.
while j >= 0 and records[j][0] > item[0]:
records[j + 1] = records[j]
j -= 1
records[j + 1] = item
records = [(80, "Ana"), (70, "Ben"), (80, "Cara")]
stable_insertion_sort(records)
print(records)The output is [(70, 'Ben'), (80, 'Ana'), (80, 'Cara')]. The strict > comparison is important. Using >= would move an equal item past an earlier equal item and could destroy stability.
Adaptivity
An algorithm is adaptive if it becomes faster when the input is already partly sorted. Insertion sort is adaptive. If the array is already sorted, each item needs only one comparison, so the running time is \(O(n)\). Its worst-case time is \(O(n^2)\), usually when the array is reverse sorted.
Selection sort performs nearly the same number of comparisons regardless of input order, so it is not adaptive. Standard merge sort is also generally \(O(n \log n)\) for sorted and unsorted input.
Comparing Common Choices
| Algorithm | Stable | In-place | Adaptive |
|---|---|---|---|
| Insertion sort | Yes | Yes | Yes |
| Selection sort | Usually no | Yes | No |
| Bubble sort | Yes | Yes | Yes, in optimized form |
| Merge sort | Yes | Usually no | Usually no |
| Quick sort | Usually no | Usually yes | Usually no |
These properties are independent. A stable algorithm need not be in-place, and an in-place algorithm need not be stable. In a GATE question, inspect the implementation rather than relying only on the algorithm’s name.
Exercise
Why does changing records[j][0] > item[0] to records[j][0] >= item[0] threaten stability in insertion sort?
Show answer
It shifts an equal-key record to the right, allowing the current record to move before it. Equal records can therefore change their original relative order.