Computer Science

Consider the following unsorted list:
105, 99, 10, 43, 62, 8.
Write the passes of bubble sort for sorting the list in ascending order till the 3rd iteration.

Python Sorting

3 Likes

Answer

The unsorted list: [105, 99, 10, 43, 62, 8]

First pass — [99, 10, 43, 62, 8, 105]
Second pass — [10, 43, 62, 8, 99, 105]
Third pass — [10, 43, 8, 62, 99, 105]

Explanation

First pass

[105, 99, 10, 43, 62, 8] → [99, 105, 10, 43, 62, 8] — it will swap since 99 < 105
[99, 105, 10, 43, 62, 8] → [99, 10, 105, 43, 62, 8] — it will swap since 10 < 105
[99, 10, 105, 43, 62, 8] → [99, 10, 43, 105, 62, 8] — it will swap since 43 < 105
[99, 10, 43, 105, 62, 8] → [99, 10, 43, 62, 105, 8] — it will swap since 62 < 105
[99, 10, 43, 62, 105, 8] → [99, 10, 43, 62, 8, 105] — it will swap since 8 < 105

Second pass

[99, 10, 43, 62, 8, 105] → [10, 99, 43, 62, 8, 105] — it will swap since 10 < 99
[10, 99, 43, 62, 8, 105] → [10, 43, 99, 62, 8, 105] — it will swap since 43 < 99
[10, 43, 99, 62, 8, 105] → [10, 43, 62, 99, 8, 105] — it will swap since 62 < 99
[10, 43, 62, 99, 8, 105] → [10, 43, 62, 8, 99, 105] — it will swap since 8 < 99

Third pass

[10, 43, 62, 8, 99, 105] → [10, 43, 62, 8, 99, 105] — no swapping as 43 > 10
[10, 43, 62, 8, 99, 105] → [10, 43, 62, 8, 99, 105] — no swapping as 62 > 43
[10, 43, 62, 8, 99, 105] → [10, 43, 8, 62, 99, 105] — it will swap since 8 < 62

Answered By

3 Likes


Related Questions