r/cs50 • u/Then-Battle-7167 • 5h ago
CS50x Why does Selection sort take less time than Bubble sort In the average case?
"Although both selection sort and bubble sort share an average time complexity of O(n2), selection sort is often faster in practice. Big-O notation only describes asymptotic growth as input sizes increase and intentionally ignores constant factors. The key difference lies in the number of write and swap operations: selection sort performs at most one swap per full pass over the remaining elements, whereas bubble sort can perform numerous swaps during a single pass whenever adjacent elements are out of order. Because writing and swapping memory values is generally more computationally expensive than simply reading and comparing them, bubble sort's higher frequency of swaps results in a larger constant factor, making selection sort noticeably more efficient overall."