An Interest In:
Web News this Week
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
June 20, 2021 08:19 pm GMT
Original Link: https://dev.to/ayabouchiha/bubble-sort-algorithm-516f
Bubble sort algorithm
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
Original Link: https://dev.to/ayabouchiha/bubble-sort-algorithm-516f
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To