Easy Algorithms Sorting Algorithms
How does bubble sort work and what is its complexity?
Answer
Bubble sort repeatedly steps through the array, comparing adjacent elements and swapping them if they're in wrong order. The largest elements 'bubble' to the end in each pass. Time complexity is O(n^2) in average and worst case (already sorted in reverse), O(n) in best case (already sorted with optimization flag). Space is O(1). Simple but inefficient for large datasets; mainly used for educational purposes.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Junior Developer Student