-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.py
More file actions
31 lines (27 loc) · 1.61 KB
/
bubbleSort.py
File metadata and controls
31 lines (27 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# link: https://www.algoexpert.io/questions/Bubble%20Sort
# Bubble Sort: start from first num and compare it to the next num, if current num is greater than next num, swap 2 nums,
# and do the same thing for the rest of the array; and repeat the process until the entire array is sorted
# Write a function that takes in an array of integers and returns a sorted version of that array. Use the Bubble Sort
# algorithm to sort the array.
# If you're unfamiliar with Bubble Sort, we recommend watching the Conceptual Overview section of this question's video
# explanation before starting to code.
# Sample input: [8, 5, 2, 9, 5, 6, 3]
# Sample output: [2, 3, 5, 5, 6, 8, 9]
# Time: O(n^2), average/worst case, it runs n times each loop
# Space: O(1), constant space, since no extra space added
def bubbleSort(array):
isSorted = False # assuming array is not sorted
counter = 0 # use it to skip last (sorted) num in each loop, since we know last num is always sorted
while not isSorted: # while array is not sorted
isSorted = True # assuming array is sorted
for i in range(len(array) -1-counter): # len(array)-1: b/c i is comparing with i+1, last item would not be
# compared; -counter: - last num
if array[i] > array[i+1]: # if current num > next num
swap(i, i+1, array) # helper function to swap i and i+1
isSorted = False # array is not sorted in this if loop
counter += 1
# print(array)
return array
def swap(i, j, array):
array[i], array[j] = array[j], array[i]
# bubbleSort([8, 5, 2, 9, 5, 6, 3])