This content originally appeared on DEV Community and was authored by Aya Bouchiha
Definition of bubble sort algorithm
Bubble Sort is a type of sorting algorithms that works by comparing each pair of adjacent items and swapping them if they are in the wrong order.
Space and Time complexity of bubble sort
Time complexity | Space complexity |
---|---|
О(n2) | O(1) |
Bubble sort implementation using python
def BubbleSortAlgorithm(items: list) -> list:
"""
[name] => Bubble Sort
[type] => Sorting algorithms
[space complexity] => O(1)
[time complexity] => O(n^2)
@params (
[items] => list
)
@return => sorted list
"""
for i in range(len(items) - 1):
isSorted = True
for j in range(len(items) - i - 1):
# if the number is greater than the adjacent element
if items[j] > items[j + 1] :
# swap
items[j], items[j + 1] = items[j + 1], items[j]
isSorted = False
# if the list is sorted
if isSorted:
break
return items
References and useful resources
- https://www.geeksforgeeks.org/python-program-for-bubble-sort/
- https://www.techopedia.com/definition/3757/bubble-sort
- https://www.geeksforgeeks.org/bubble-sort/
This content originally appeared on DEV Community and was authored by Aya Bouchiha
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Aya Bouchiha | Sciencx (2021-06-20T20:19:38+00:00) Bubble sort algorithm. Retrieved from https://www.scien.cx/2021/06/20/bubble-sort-algorithm/
" » Bubble sort algorithm." Aya Bouchiha | Sciencx - Sunday June 20, 2021, https://www.scien.cx/2021/06/20/bubble-sort-algorithm/
HARVARDAya Bouchiha | Sciencx Sunday June 20, 2021 » Bubble sort algorithm., viewed ,<https://www.scien.cx/2021/06/20/bubble-sort-algorithm/>
VANCOUVERAya Bouchiha | Sciencx - » Bubble sort algorithm. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/20/bubble-sort-algorithm/
CHICAGO" » Bubble sort algorithm." Aya Bouchiha | Sciencx - Accessed . https://www.scien.cx/2021/06/20/bubble-sort-algorithm/
IEEE" » Bubble sort algorithm." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/06/20/bubble-sort-algorithm/. [Accessed: ]
rf:citation » Bubble sort algorithm | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/06/20/bubble-sort-algorithm/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.