Question:
Write a program to perform bubble sort on [85,63,0,12,47,96,52]
Program:
1 2 3 4 5 6 7 8 9 10 11 |
def bubbleSort(alist): for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp alist = [85,63,0,12,47,96,52] bubbleSort(alist) print(alist) |
Explanation:
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.
Output:
1 |
[0, 12, 47, 52, 63, 85, 96] |
Leave a Reply