0% found this document useful (0 votes)
21 views

Bubble - Sort - Ipynb - Colab

The bubble sort algorithm repeatedly steps through a list, compares adjacent elements, and swaps them if they are in the wrong order until the list is fully sorted. It has a worst-case and average-case time complexity of O(n^2) due to potentially making n(n-1)/2 comparisons in each pass through the list. An optimized version with an early exit can reduce the best-case complexity to O(n).

Uploaded by

MH Moin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Bubble - Sort - Ipynb - Colab

The bubble sort algorithm repeatedly steps through a list, compares adjacent elements, and swaps them if they are in the wrong order until the list is fully sorted. It has a worst-case and average-case time complexity of O(n^2) due to potentially making n(n-1)/2 comparisons in each pass through the list. An optimized version with an early exit can reduce the best-case complexity to O(n).

Uploaded by

MH Moin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

keyboard_arrow_down Bubble Sort Algorithm

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

1. Start at the beginning of the list.


2. Compare the first two elements.

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

Consider the following list: [5, 3, 8, 4, 2]

Pass 1:

Compare 5 and 3: swap → [3, 5, 8, 4, 2]


Compare 5 and 8: no swap → [3, 5, 8, 4, 2]
Compare 8 and 4: swap → [3, 5, 4, 8, 2]
Compare 8 and 2: swap → [3, 5, 4, 2, 8]

Pass 2:

Compare 3 and 5: no swap → [3, 5, 4, 2, 8]


Compare 5 and 4: swap → [3, 4, 5, 2, 8]
Compare 5 and 2: swap → [3, 4, 2, 5, 8]

Pass 3:

Compare 3 and 4: no swap → [3, 4, 2, 5, 8]


Compare 4 and 2: swap → [3, 2, 4, 5, 8]
Pass 4:

Compare 3 and 2: swap → [2, 3, 4, 5, 8]

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]

keyboard_arrow_down Time Complexity Analysis


Outer Loop

for i in range(n-1, 0, -1):

This loop runs from n-1 to 1 .


It runs n-1 times.

Inner Loop

for x in range(0, i):

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

Time Complexity Calculation


Worst-Case Time Complexity:
n2 −n
The number of comparisons in the worst case is
2
When considering Big- O notation, we focus on the term with the highest growth rate and drop
constant factors and lower- order terms.
Therefore, the worst- case time complexity is O(n2 )
Best-Case Time Complexity

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 )

keyboard_arrow_down Early exit optimization


If we optimize the bubble sort with an early exit flag, the best-case time complexity can be reduced
to O(n)

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]

You might also like