Last active
October 7, 2020 23:25
-
-
Save skatesham/201fcdacda8fa1728bdd061556cfa4f9 to your computer and use it in GitHub Desktop.
Simples bobble_sort Sort Algorithm
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
import unittest | |
def bobble_sort(values): | |
""" | |
Sorting list algorithm type of BobbleSort. | |
Time O(n**2 - n) | |
Space O(n + 3) | |
:author sham vinicius fiorin | |
:param values: List of comparable objects | |
:return: ordered list | |
""" | |
for i in range(len(values)): | |
pivotIdx = 0 | |
for actualIdx in range(len(values)): | |
if pivotIdx == actualIdx: | |
continue | |
if values[pivotIdx] > values[actualIdx]: | |
values[pivotIdx], values[actualIdx] = values[actualIdx], values[pivotIdx] | |
pivotIdx += 1 | |
return values | |
class BobbleSortTest(unittest.TestCase): | |
def teste_lista_vazia(self): | |
self.assertListEqual([], bobbleSort([])) | |
def teste_lista_unitaria(self): | |
self.assertListEqual([1], bobbleSort([1])) | |
def teste_lista_binaria(self): | |
self.assertListEqual([1, 2], bobbleSort([2, 1])) | |
def teste_lista_desordenada(self): | |
self.assertListEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], bobbleSort([9, 7, 1, 8, 5, 3, 6, 4, 2, 0])) | |
def teste_lista_com_elementos_repetidos(self): | |
self.assertListEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9], bobbleSort([9, 7, 1, 8, 5, 3, 6, 4, 2, 0, 9, 9])) | |
def teste_lista_so_com_elementos_repetidos(self): | |
self.assertListEqual([9, 9, 9], bobbleSort([9, 9, 9])) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment