Bubble sort is one of the easiest sorting method out there. Student can start learning sorting algorithm from this basic sort. It is great for beginner as well.
def bubbleSort(collection): for value in range(len(collection)-1,0,-1): for i in range(value): if collection[i]>collection[i+1]: temp = collection[i] collection[i] = collection[i+1] collection[i+1] = temp # Author : ITVoyagers Website : itvoyagers.in collection = [1200,1500,1600,1800,1900,1700,1400] print("collection before sort", collection) bubbleSort(collection) print("sorted collection is", collection) # Author : ITVoyagers Website : itvoyagers.in
collection before sort [1200, 1500, 1600, 1800, 1900, 1700, 1400] sorted collection is [1200, 1400, 1500, 1600, 1700, 1800, 1900]
Note : Please note that above program is compatible with Python 3 or higher version.
Please check out other practical related to Data structures