Back to Dashboard
Bubble Sort
The most intuitive sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
FastSlow
Initializing...
Time Complexity
Best
Average
Worst
Space Complexity
StabilityTRUE
In-placeTRUE
Explanation
What is Bubble Sort?
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm is named for the way larger elements "bubble" up to the end of the list.
Algorithm Steps
- Compare two adjacent values from the beginning of the array.
- If the left value is higher than the right, swap them.
- Repeat this until the end of the array. After the first pass, the largest element is placed at the right end.
- Repeat the above for the unsorted part of the array.
Time Complexity
- Worstcase:
- Average:
- Bestcase: (when already sorted)
Pseudo Code
1procedure bubbleSort(list)2 for i from 0 to list.length - 13 for j from 0 to list.length - i - 14 if list[j] > list[j + 1]5 swap(list[j], list[j + 1])6end procedure
Sponsor Ad