1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-10-29 22:17:23 +01:00

feat(solutions): add sorting-algorithms/python/bubble-sort

This commit is contained in:
Divlo 2021-06-29 20:38:10 +02:00
parent 0c441c2e9d
commit d45453e00a
No known key found for this signature in database
GPG Key ID: 185ED2F15F104E52
2 changed files with 30 additions and 0 deletions

View File

@ -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²) |

View File

@ -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)