@@ -19,28 +19,28 @@ Sort `[5, 1, 4, 2, 8]`
1919
2020** First Pass**
2121 ``` kotlin
22- [5 , 1 , 4 , 2 , 8 ] -> [1 , 5 , 4 , 2 , 8 ] Compares the first two elements, and swap since 5 > 1
23- [1 , 5 , 4 , 2 , 8 ] -> [1 , 4 , 5 , 2 , 8 ] Swap since 5 > 4
24- [1 , 4 , 5 , 2 , 8 ] -> [1 , 4 , 2 , 5 , 8 ] Swap since 5 > 2
25- [1 , 4 , 2 , 5 , 8 ] -> [1 , 4 , 2 , 5 , 8 ] Now , since these elements are already in order (8 > 5 ), algorithm does not swap them
22+ [5 , 1 , 4 , 2 , 8 ] // [1, 5, 4, 2, 8] Compares the first two elements, and swap since 5 > 1
23+ [1 , 5 , 4 , 2 , 8 ] // [1, 4, 5, 2, 8] Swap since 5 > 4
24+ [1 , 4 , 5 , 2 , 8 ] // [1, 4, 2, 5, 8] Swap since 5 > 2
25+ [1 , 4 , 2 , 5 , 8 ] // [1, 4, 2, 5, 8] Now, since these elements are already in order (8 > 5), algorithm does not swap them
2626```
2727
2828** Second Pass**
2929``` kotlin
30- [1 , 4 , 2 , 5 , 8 ] -> [1 , 4 , 2 , 5 , 8 ]
31- [1 , 4 , 2 , 5 , 8 ] -> [1 , 2 , 4 , 5 , 8 ] Swap since 4 > 2
32- [1 , 2 , 4 , 5 , 8 ] -> [1 , 2 , 4 , 5 , 8 ]
33- [1 , 2 , 4 , 5 , 8 ] -> [1 , 2 , 4 , 5 , 8 ]
30+ [1 , 4 , 2 , 5 , 8 ] // [1, 4, 2, 5, 8]
31+ [1 , 4 , 2 , 5 , 8 ] // [1, 2, 4, 5, 8] Swap since 4 > 2
32+ [1 , 2 , 4 , 5 , 8 ] // [1, 2, 4, 5, 8]
33+ [1 , 2 , 4 , 5 , 8 ] // [1, 2, 4, 5, 8]
3434```
3535
3636Now, the list is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass
3737without any swap to know it is sorted
3838
3939** Third Pass**
4040``` kotlin
41- [1 , 2 , 4 , 5 , 8 ] -> [1 , 2 , 4 , 5 , 8 ]
42- [1 , 2 , 4 , 5 , 8 ] -> [1 , 2 , 4 , 5 , 8 ]
43- [1 , 2 , 4 , 5 , 8 ] -> [1 , 2 , 4 , 5 , 8 ]
44- [1 , 2 , 4 , 5 , 8 ] -> [1 , 2 , 4 , 5 , 8 ]
41+ [1 , 2 , 4 , 5 , 8 ] // [1, 2, 4, 5, 8]
42+ [1 , 2 , 4 , 5 , 8 ] // [1, 2, 4, 5, 8]
43+ [1 , 2 , 4 , 5 , 8 ] // [1, 2, 4, 5, 8]
44+ [1 , 2 , 4 , 5 , 8 ] // [1, 2, 4, 5, 8]
4545```
4646
0 commit comments