Need help from an expert?
The world’s top online tutoring provider trusted by students, parents, and schools globally.
Bubble sort works by repeatedly swapping adjacent elements if they are in the wrong order until the list is sorted.
Bubble sort is a simple sorting algorithm that is used in computer science. It's named 'bubble sort' because with each iteration, the largest unsorted element 'bubbles' up to its correct position. This algorithm is not suitable for large data sets as its average and worst-case time complexity are both O(n²), where n is the number of items being sorted.
The process begins at the start of the list, comparing the first and second elements. If the first element is larger than the second, they are swapped. The algorithm then moves on to compare the second and third elements, and so on. This continues until the end of the list is reached. At this point, the largest element will have been moved to the end of the list, in its correct position. This is known as one pass.
The algorithm then starts again from the beginning of the list, repeating the same process. However, it doesn't need to go all the way to the end this time, as the last element is already in its correct position. With each pass, the range of elements that need to be checked reduces by one.
This process continues until a pass is made with no swaps, indicating that the list is now fully sorted. In the best-case scenario, where the list is already sorted, bubble sort makes a single pass through the list and makes no swaps, giving it a best-case time complexity of O(n).
Bubble sort is not often used in practice due to its inefficiency compared to other sorting algorithms. However, it is simple to understand and implement, making it a good starting point for learning about sorting algorithms. It also has the advantage of being able to detect whether a list is already sorted, allowing it to potentially finish early in certain situations.
Study and Practice for Free
Trusted by 100,000+ Students Worldwide
Achieve Top Grades in your Exams with our Free Resources.
Practice Questions, Study Notes, and Past Exam Papers for all Subjects!
The world’s top online tutoring provider trusted by students, parents, and schools globally.