Bubble Sort
00:00
TCSWiproInfosys
Sort an integer array using bubble sort — repeatedly compare adjacent elements and swap if in wrong order. Optimize with early exit if no swaps occur.
Examples
Input → [64,34,25,12,22,11,90]
Output → [11,12,22,25,34,64,90]
Input → [5,1,4,2,8]
Output → [1,2,4,5,8]
Input → [1,2,3]
Output → [1,2,3]
Note: already sorted, 0 swaps