-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.py
53 lines (47 loc) · 1.87 KB
/
bubblesort.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#Description: Recursive and Iterative Bubble Sort Implementations
#Author: Summer Gautier
#Date: May 10th 2020
import unittest
#Recursive
def recursiveSort(listOfItems:list, size: int)-> list:
if(size == 1):
return listOfItems
#do a single pass of the bubble sort algorithm
for index,item in enumerate(listOfItems):
#if this is the last array index, skip this iteration
if(index == (len(listOfItems)-1)):
continue
#if the item is greater than the next, it needs to swap
if(item > listOfItems[index + 1]):
swapRequired = True
listOfItems = swap(listOfItems, index, index + 1)
return recursiveSort(listOfItems, size-1)
#Iterative
def iterativeSort(listOfItems: list) -> list:
while(True):
swapRequired: bool = False
for index,item in enumerate(listOfItems):
#if this is the last array index, skip this iteration
if(index == (len(listOfItems)-1)):
continue
#if the item is greater than the next, it needs to swap
if(item > listOfItems[index + 1]):
swapRequired = True
listOfItems = swap(listOfItems, index, index + 1)
if(swapRequired == False):
break
return listOfItems
#helper method
def swap(listOfItems: list, firstIndex: int, secondIndex: int) -> list:
temp: int = listOfItems[firstIndex]
listOfItems[firstIndex] = listOfItems[secondIndex]
listOfItems[secondIndex] = temp
return listOfItems
#TEST SORT METHODS
class SortTest(unittest.TestCase):
def test(self):
sampleList: list = [7,5,28,16,9,14,25]
self.assertEqual(iterativeSort(sampleList), [5,7,9,14,16,25,28])
self.assertEqual(recursiveSort(sampleList, len(sampleList)), [5,7,9,14,16,25,28])
if __name__ == '__main__':
print(str(unittest.main()))