1def bubbleSort(arr):
2 n = len(arr)
3
4 # Traverse through all array elements
5 for i in range(n-1):
6 # range(n) also work but outer loop will repeat one time more than needed.
7
8 # Last i elements are already in place
9 for j in range(0, n-i-1):
10
11 # traverse the array from 0 to n-i-1
12 # Swap if the element found is greater
13 # than the next element
14 if arr[j] > arr[j+1] :
15 arr[j], arr[j+1] = arr[j+1], arr[j]
16
17# Driver code to test above
18arr = [64, 34, 25, 12, 22, 11, 90]
19
20bubbleSort(arr)
19 Sorting Algorithms implementation in Python at this link:
2
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/Sorting%20Algorithms
1# The algorithm used by Python's sort() and sorted() is known as Timsort.
2# This algorithm is based on Insertion sort and Merge sort.
3# A stable sorting algorithm works in O(n Log n) time.
4# Used in Java’s Arrays.sort() as well.
5
6# The array is divided into blocks called Runs.
7# These Runs are sorted using Insertion sort and then merged using Merge sort.
8
9arr = [6, 2, 8, 9, 5, 3, 0, 15]
10arr.sort() # Since sort() does inplace sorting and returns None
11print(arr)
12
13arr = [6, 2, 8, 9, 5, 3, 0, 15]
14print(sorted(arr)) # sorted() returns the sorted array