Image for post
Image for post

Bubble Sort Overview

Best Case

The procedure or algorithm that we used is below:

procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
Get the C code here: https://github.com/randerson112358/C-Programs/blob/master/BubbleSort2.c

Let’s take a look at a different implementation of the bubble sort algorithm:

BUBBLESORT(A)
1 for i = 1 to A.length - 1
2 for j = A.length downto i + 1
3 if A[j] < A[j - 1]
4 exchange A[j] with A[j - 1]

Worst Case

Get the code here:
https://github.com/randerson112358/C-Programs/blob/master/BubbleSort.c
Image for post
Cracking The Coding Interview

Check Out the following for more content / videos on Algorithm Analysis and Programming:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store