Lecture 35
Lecture 35
Lecture 35
Lecture Outline
• Additional example
– CS1001 Lecture 35 –
Bubble Sort
3, 1, 2, 5, 4, 2, 1, 9
1, 3, 2, 5, 4, 2, 1, 9
– CS1001 Lecture 35 – 1
1, 2, 3, 5, 4, 2, 1, 9
1, 2, 3, 4, 2, 1, 5, 9
1, 2, 3, 2, 1, 4, 5, 9
1, 1, 2, 2, 3, 4, 5, 9
– CS1001 Lecture 35 – 2
Bubble Sort
# This function creates a list of length random numbers
# between 5 and 100.
import random
def main():
lst = []
randList(lst, 6)
print("List of random numbers:")
– CS1001 Lecture 35 – 3
print(lst)
bubbleSort(lst)
print()
print("Sorted list:")
print(lst)
main()
– CS1001 Lecture 35 – 4
Bubble Sort
Sample output:
List of random numbers:
[80, 24, 40, 89, 7, 45]
Traversal 1
Swapping elements 1 and 2
[80, 40, 24, 89, 7, 45]
Swapping elements 2 and 3
[80, 40, 89, 24, 7, 45]
Swapping elements 4 and 5
[80, 40, 89, 24, 45, 7]
Traversal 2
Swapping elements 1 and 2
[80, 89, 40, 24, 45, 7]
Swapping elements 3 and 4
[80, 89, 40, 45, 24, 7]
Traversal 3
Swapping elements 0 and 1
[89, 80, 40, 45, 24, 7]
Swapping elements 2 and 3
[89, 80, 45, 40, 24, 7]
Traversal 4
Sorted list:
[89, 80, 45, 40, 24, 7]
– CS1001 Lecture 35 – 5