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.
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
Related Questions
Write a Python program to sort a list alphabetically in a dictionary.
Write a Python program to count number of items in a dictionary value that is a list.
What will be the status of the following list after the First, Second and Third pass of the insertion sort method used for arranging the following elements in descending order?
28, 44, 97, 34, 50, 87
Note: Show the status of all the elements after each pass very clearly underlining the changes.
Evaluate the following expression:
6 * 3 + 4 ** 2 // 5 - 8