@@ -16,59 +16,59 @@ Stability, in the context of sorting, refers to preserving the relative order of
16
16
- In an ** unstable** sorting algorithm, their order might be reversed in the sorted output.
17
17
- In a ** stable** sorting algorithm, their relative order remains unchanged.
18
18
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):
22
20
23
21
```
24
- [C#0 ] [Python1 ] [C#2 ] [JavaScript3 ] [Python4 ]
22
+ [Lancelot0 ] [Gawain1 ] [Lancelot2 ] [Percival3 ] [Gawain4 ]
25
23
```
26
24
25
+ We’ll “sort” them by name, bringing all ** Lancelot** s to the front, then ** Gawain** , then ** Percival** .
26
+
27
27
#### Stable Sort
28
28
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 .
30
30
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 ):
32
32
33
33
```
34
- [C#0 ] [C#2 ] [Python1 ] [JavaScript3 ] [Python4 ]
34
+ [Lancelot0 ] [Lancelot2 ] [Gawain1 ] [Percival3 ] [Gawain4 ]
35
35
```
36
36
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 :
38
38
39
39
```
40
- [C#0 ] [C#2 ] [JavaScript3 ] [Python1 ] [Python4 ]
40
+ [Lancelot0 ] [Lancelot2 ] [Gawain1 ] [Gawain4 ] [Percival3 ]
41
41
```
42
42
43
- So the stable-sorted sequence is:
43
+ So the ** stable-sorted** sequence is:
44
44
45
45
```
46
- [C#0 ] [C#2 ] [JavaScript3 ] [Python1 ] [Python4 ]
46
+ [Lancelot0 ] [Lancelot2 ] [Gawain1 ] [Gawain4 ] [Percival3 ]
47
47
```
48
48
49
49
#### Unstable Sort
50
50
51
- An ** unstable** sort does * not * guarantee that equal items keep their original relative order .
51
+ An ** unstable** sort may reorder equal items arbitrarily .
52
52
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 :
54
54
55
55
```
56
- [C#2 ] [C#0 ] [Python1 ] [JavaScript3 ] [Python4 ]
56
+ [Lancelot2 ] [Lancelot0 ] [Gawain1 ] [Percival3 ] [Gawain4 ]
57
57
```
58
58
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 ):
60
60
61
61
```
62
- [C#2 ] [C#0 ] [JavaScript3 ] [Python4 ] [Python1 ]
62
+ [Lancelot2 ] [Lancelot0 ] [Gawain4 ] [Gawain1 ] [Percival3 ]
63
63
```
64
64
65
- So one possible unstable-sorted sequence is:
65
+ So one possible ** unstable-sorted** sequence is:
66
66
67
67
```
68
- [C#2 ] [C#0 ] [JavaScript3 ] [Python4 ] [Python1 ]
68
+ [Lancelot2 ] [Lancelot0 ] [Gawain4 ] [Gawain1 ] [Percival3 ]
69
69
```
70
70
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 .
72
72
73
73
### Bubble Sort
74
74
0 commit comments