asked 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?

asked
User Tsaebeht
by
8.4k 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




answered
User Eric Yin
by
8.6k points
Welcome to Qamnty — a place to ask, share, and grow together. Join our community and get real answers from real people.