Computer Applications
Sam performs bubble sort on the following array to organise the elements in ascending order:
{5, 1, 2, 3}
After the first comparison the array is:
{1, 5, 2, 3}
What would be the array after the next comparison?
- {1, 2, 5, 3}
- {1, 5, 3, 2}
- {1, 3, 5, 2}
- {1, 3, 2, 5}
Java Arrays
2 Likes
Answer
{1, 2, 5, 3}
Reason — In Bubble Sort, adjacent elements are compared and swapped if they are in the wrong order.
Original array: {5, 1, 2, 3}
After the first comparison (compare 5 and 1):
Since 5 > 1, they are swapped → {1, 5, 2, 3}
Next comparison (second comparison):
Compare 5 and 2. Since 5 > 2, they are swapped → {1, 2, 5, 3}
So, after the next comparison, the array becomes {1, 2, 5, 3}.
Answered By
2 Likes
Related Questions
Choose the statement which is equivalent to the given:
if(a>b) System.out.println("Honest"); else System.out.println("Sincere");a>b? System.out.println("Honest"):System.out.println("Sincere");System.out.println(a>b? "Honest":"Sincere");a>b? "Honest": "Sincere";a>b: "Honest"? "Sincere";
The two Java statement are used to check for the equality of the strings "COP" and "cop" are as follows:
"COP".equals("cop") "COP".equalsIgnoreCase("cop")The output of the above statements is:
- false, true
- true, false
- false, false
- true, true
The output of the java statement
"SOLDIER".compareTo("SOLUTE");is- -4
- -17
- 17
- 0
Arrange the following in the descending order of number of bytes occupied.
(i) char x[20]
(ii) int a[4][2]
(iii) double b[6]- (iii), (ii), (i)
- (iii), (i), (ii)
- (ii), (iii), (i)
- (i), (ii), (iii)