119k views
4 votes
Suppose you have this array of five integers: int[] list {2, 5, 4, 8, 3}. if a bubble sort is being used, which index of the array will the number 8 be in after the first pass through the algorithm?

User Tsaebeht
by
6.0k points

1 Answer

2 votes
Array list: ⇒ 2, 5, 4, 8, 3

Sort the array from lowest number to greatest number using bubble sort.

First pass:
2 , 5 , 4 , 8 , 3
2 , 4 , 5 , 8 , 3 ⇒⇒⇒ Swap since 5 > 4
2 , 4 , 5 , 8 , 3
2 , 4 , 5 , 3 , 8 ⇒⇒⇒ Swap since 8 > 3

∴ The index of the number 8 after the first pass = 5




User Eric Yin
by
6.5k points