Asymptotic Complexity | |
Time Complexity | Space Complexity |
n^2 | 1 |
If you would like to see how this implentation would look like in C click the following link: Bubblesort implementation in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/env/path python | |
arr = [6,5,4,7,8,4,3,2,1,45,23,67,34] | |
def bubble_sort(arr): | |
swapped, j = True, 1 | |
length = len(arr) | |
while(swapped): | |
swapped = False | |
for i in range(0,length-j): | |
if( arr[i] >= arr[i+1] ): | |
temp = arr[i] | |
arr[i] = arr[i+1] | |
arr[i+1] = temp | |
swapped = True | |
j += 1 | |
print(arr) | |
bubble_sort(arr) | |
print(arr) |