@@ -19,28 +19,28 @@ Sort `[5, 1, 4, 2, 8]`
19
19
20
20
** First Pass**
21
21
``` 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
26
26
```
27
27
28
28
** Second Pass**
29
29
``` 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]
34
34
```
35
35
36
36
Now, the list is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass
37
37
without any swap to know it is sorted
38
38
39
39
** Third Pass**
40
40
``` 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]
45
45
```
46
46
0 commit comments