Skip to content

Latest commit

 

History

History
26 lines (19 loc) · 582 Bytes

bubble_sort in python.md

File metadata and controls

26 lines (19 loc) · 582 Bytes

Bubble sort in python

Description

Bubble sort just iterate through the array at least n times (n is the number of the elements of the array to sort) and compares eache pair of asjacent items and swaps them if they are in the wrong order.

Worst Case Complexity: n^2^

Code

# List to sort
L = [5, 3, 2, 1, 6]

def bubble_sort(List):
	COMPLETED = False
	while not COMPLETED:
		COMPLETED = True
		for i in range(len(L) - 1):
			if List[i] > List[i+1]:
				COMPLETED = False
				List[i], List[i+1] = List[i+1], List[i]
	return List

print(bubble_sort(L))