Bubble - Sort - Ipynb - Colab
Bubble - Sort - Ipynb - Colab
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. This process is repeated until the list is
sorted.
Step-by-Step Explanation
If the first element is greater than the second element, swap them.
If the first element is not greater than the second element, do nothing.
3. Move to the next pair of elements and repeat the comparison and swap if necessary.
4. Continue this process for each pair of adjacent elements to the end of the list.
5. After each pass through the list, the largest element will have bubbled up to its correct
position at the end of the list.
6. Repeat the entire process for the rest of the list (excluding the last sorted elements) until
no more swaps are needed.
Example
Pass 1:
Pass 2:
Pass 3:
target_list = [5,3,8,4,2]
# target_list = [85, 31, 14, 67, 92, 21, 48, 75, 39, 56]
# target_list = [93, 3, 77, 19, 84, 57, 12, 45, 98, 63, 29, 74, 91, 40, 22, 50, 36, 64,
# target_list = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# target_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# target_list = [-1000000, 0, 1000000, -999999, 999999, -1, 1]
# target_list = [1000000, 999999, 1000001, 500000, 999998, 1000002]
n = len(target_list)
for i in range(n-1, 0, -1):
for x in range(0, i):
if (target_list[x] > target_list[x+1] ):
temp = target_list[x]
target_list[x] = target_list[x+1]
target_list[x+1] = temp
print(target_list)
[2, 3, 4, 5, 8]
Inner Loop
For each iteration of the outer loop, the inner loop runs from 0 to i-1 .
When i = n-1 , the inner loop runs n-1 times.
When i = n-2 , the inner loop runs n-2 times.
...
When i = 1 , the inner loop runs 1 time.
Total Comparisons
The total number of comparisons made by the inner loop can be calculated by summing up the
number of comparisons in each pass of the outer loop:
(n − 1) + (n − 2) + (n − 3) + …
+1
We can rewrite it as follows:
1 + 2 + 3+. . . +(n − 1)
The formula for the sum of the first m natural numbers is:
n−1 m(m+1)
∑k=1 k = 2
In our case m = n − 1 , so we substitute m with n − 1:
n(n−1)
∑n−1
k=1 k = 2
In the best case (where the list is already sorted), the provided code will still perform all the
comparisons as it does not include an early exit optimization.
Thus, the best- case time complexity, without optimization, remains O(n2 )
Average-Case Time Complexity
The average- case time complexity also involves considering all possible permutations of the
list and their likelihood, which results in a similar number of comparisons and swaps as in the
worst case.
Therefore, the average- case time complexity is O(n2 )
In this optimized version, the best-case time complexity is O(n) when the list is already sorted,
because the algorithm will terminate early if no swaps are made in a pass.
target_list = [5,3,8,4,2]
n = len(target_list)
for i in range(n-1, 0, -1):
swapped = False
for x in range(0, i):
if (target_list[x] > target_list[x+1] ):
temp = target_list[x]
target_list[x] = target_list[x+1]
target_list[x+1] = temp
swapped = True
if not swapped:
break
print(target_list)
[2, 3, 4, 5, 8]