mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add sorting-algorithms/python/bubble-sort
This commit is contained in:
parent
0c441c2e9d
commit
d45453e00a
@ -0,0 +1,7 @@
|
||||
# sorting-algorithms/python/bubble-sort
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 29 June 2021.
|
||||
|
||||
| Algorithm | Best Case | Average Case | Worst Case |
|
||||
| ----------------------------------------------------------- | ----------- | ------------ | ----------- |
|
||||
| [Bubble sort](https://wikipedia.org/wiki/Bubble_sort) | O(n) | O(n²) | O(n²) |
|
@ -0,0 +1,23 @@
|
||||
from typing import List, Any
|
||||
import sys
|
||||
|
||||
|
||||
def bubble_sort(numbersInput: List[int]) -> List[int]:
|
||||
numbers = list(numbersInput)
|
||||
length = len(numbers)
|
||||
for index_1 in range(length):
|
||||
for index_2 in range(length - index_1 - 1):
|
||||
if numbers[index_2] > numbers[index_2 + 1]:
|
||||
temporary = numbers[index_2]
|
||||
numbers[index_2] = numbers[index_2 + 1]
|
||||
numbers[index_2 + 1] = temporary
|
||||
return numbers
|
||||
|
||||
|
||||
numbers: List[int] = []
|
||||
for value in sys.stdin:
|
||||
numbers.append(int(value.rstrip('\n')))
|
||||
|
||||
sorted_numbers = bubble_sort(numbers)
|
||||
for number in sorted_numbers:
|
||||
print(number)
|
Loading…
Reference in New Issue
Block a user