@@ -16,59 +16,59 @@ Stability, in the context of sorting, refers to preserving the relative order of
1616- In an ** unstable** sorting algorithm, their order might be reversed in the sorted output.
1717- In a ** stable** sorting algorithm, their relative order remains unchanged.
1818
19- #### Example
20-
21- Picture each element as a ** programming-language name** followed by the 0-based position it held in the original (unsorted) list:
19+ Let’s take an analogous list of medieval knights (each with its original 0-based index):
2220
2321```
24- [C#0 ] [Python1 ] [C#2 ] [JavaScript3 ] [Python4 ]
22+ [Lancelot0 ] [Gawain1 ] [Lancelot2 ] [Percival3 ] [Gawain4 ]
2523```
2624
25+ We’ll “sort” them by name, bringing all ** Lancelot** s to the front, then ** Gawain** , then ** Percival** .
26+
2727#### Stable Sort
2828
29- A ** stable** sort keeps items that compare as “equal” in the same left-to-right order they started with .
29+ A ** stable** sort preserves the left-to-right order of equal items .
3030
31- I. Bring every ` "C#" ` to the front ** without ** changing their internal order ( ` 0 ` still precedes ` 2 ` ):
31+ I. ** Bring every “Lancelot” to the front** , in the order they appeared (index 0 before 2 ):
3232
3333```
34- [C#0 ] [C#2 ] [Python1 ] [JavaScript3 ] [Python4 ]
34+ [Lancelot0 ] [Lancelot2 ] [Gawain1 ] [Percival3 ] [Gawain4 ]
3535```
3636
37- II. Next, move the two ` "Python" ` entries ahead of ` "JavaScript" ` , again preserving ` 1 ` before ` 4 ` :
37+ II. ** Next, move the two “Gawain”s ** ahead of “Percival” , again preserving 1 before 4 :
3838
3939```
40- [C#0 ] [C#2 ] [JavaScript3 ] [Python1 ] [Python4 ]
40+ [Lancelot0 ] [Lancelot2 ] [Gawain1 ] [Gawain4 ] [Percival3 ]
4141```
4242
43- So the stable-sorted sequence is:
43+ So the ** stable-sorted** sequence is:
4444
4545```
46- [C#0 ] [C#2 ] [JavaScript3 ] [Python1 ] [Python4 ]
46+ [Lancelot0 ] [Lancelot2 ] [Gawain1 ] [Gawain4 ] [Percival3 ]
4747```
4848
4949#### Unstable Sort
5050
51- An ** unstable** sort does * not * guarantee that equal items keep their original relative order .
51+ An ** unstable** sort may reorder equal items arbitrarily .
5252
53- I. While collecting ` "C#" ` items, the algorithm might emit index ` 2 ` * before* index ` 0 ` :
53+ I. When collecting the “Lancelot”s, it might pick index 2 before 0 :
5454
5555```
56- [C#2 ] [C#0 ] [Python1 ] [JavaScript3 ] [Python4 ]
56+ [Lancelot2 ] [Lancelot0 ] [Gawain1 ] [Percival3 ] [Gawain4 ]
5757```
5858
59- II. Later, the two ` "Python" ` entries can also swap positions ( ` 4 ` before ` 1 ` ):
59+ II. Later, the two “Gawain”s might swap (4 before 1 ):
6060
6161```
62- [C#2 ] [C#0 ] [JavaScript3 ] [Python4 ] [Python1 ]
62+ [Lancelot2 ] [Lancelot0 ] [Gawain4 ] [Gawain1 ] [Percival3 ]
6363```
6464
65- So one possible unstable-sorted sequence is:
65+ So one possible ** unstable-sorted** sequence is:
6666
6767```
68- [C#2 ] [C#0 ] [JavaScript3 ] [Python4 ] [Python1 ]
68+ [Lancelot2 ] [Lancelot0 ] [Gawain4 ] [Gawain1 ] [Percival3 ]
6969```
7070
71- This stability property matters when you chain sorts on multiple keys—for instance, first sorting bug reports by ** severity ** , then by ** timestamp ** —because each later pass can rely on ties already being in the correct internal order.
71+ If you then did a second pass (say, sorting by rank or battle-honors) you’d only want to reorder knights of different names, trusting that ties (same-name knights) are still in their intended original order—something only a stable sort guarantees .
7272
7373### Bubble Sort
7474
0 commit comments