Bubble sort is a simple sorting algorithm that works by repeatedly swapping adjacent elements in an array that are in the wrong order. This process is repeated until the entire array is sorted. The algorithm starts at the beginning of the array and moves through it, swapping adjacent elements that are out of order. This process is repeated until the end of the array is reached, at which point the algorithm starts again from the beginning. The algorithm continues to iterate through the array and swap adjacent elements until no more swaps are needed. Although bubble sort has a time complexity of O(n^2), which makes it less efficient than other sorting algorithms, it can be useful for small arrays or as a simple illustration of how sorting algorithms work.
Algorithm | Time Complexity | Space Complexity | ||
---|---|---|---|---|
Best | Average | Worst | Worst | |
Bubble Sort | O(n) | O(n^2) | O(n^2) | O(1) |
Insertion Sort | O(n) | O(n^2) | O(n^2) | O(1) |
Selection Sort | O(n^2) | O(n^2) | O(n^2) | O(1) |
Merge Sort | O(n log(n)) | O(n log(n)) | O(n log(n)) | O(n) |
Quick Sort | O(n log(n)) | O(n log(n)) | O(n^2) | O(log (n)) |
Radix Sort | O(nk) | O(nk) | O(nk) | O(n+k) |