What is bubble sort?
Why Interviewers Ask This
This is a classic screening question for Data Structures & Algorithms roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
Bubble sort is a simple sorting algorithm that repeatedly passes through the list, compares adjacent elements, and swaps them if they're in the wrong order. Largest elements "bubble up" to the end with each pass. Algorithm: for each pass (n-1 passes total), compare each adjacent pair; if arr[j] > arr[j+1], swap them. After each pass, the largest unsorted element is in its correct position. Optimized: if no swaps occur in a pass, the array is sorted — exit early. Time: O(n²) worst/average, O(n) best (already sorted with early exit). Space: O(1) — in-place. Stable: yes (equal elements maintain relative order). Practical use: almost none — bubble sort is pedagogically simple but practically the worst sorting algorithm. Never use in production. Only advantage over selection sort: it's stable and detects sorted input in O(n). Comparison: selection sort also O(n²) but makes fewer swaps; insertion sort O(n²) worst but O(n) for nearly-sorted; merge sort O(n log n) but O(n) space; quicksort O(n log n) average, in-place. Know bubble sort for interviews to demonstrate understanding, but explain why you'd never use it.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Data Structures & Algorithms answers easy to follow.