0% found this document useful (0 votes)
8 views

Lecture 35

The document discusses bubble sort, an algorithm that sorts a list by repeatedly swapping adjacent elements that are in the wrong order. It provides an example of bubble sort being applied to a list of random numbers, with the list contents and swap steps printed at each iteration until the list is fully sorted.

Uploaded by

Maxi Brad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 35

The document discusses bubble sort, an algorithm that sorts a list by repeatedly swapping adjacent elements that are in the wrong order. It provides an example of bubble sort being applied to a list of random numbers, with the list contents and swap steps printed at each iteration until the list is fully sorted.

Uploaded by

Maxi Brad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Science 1001

Lecture 35

Lecture Outline

• Additional example

– CS1001 Lecture 35 –
Bubble Sort

• Another common sorting algorithm is called bubble


sort.

• Starting with an unsorted list, bubble sort begins by


comparing the first two values and swapping them
if the first value is larger than the second.

• Pairwise comparison of values continues to the end


of the list, swapping values as necessary.

• For example, consider the following list of 8 integers:

3, 1, 2, 5, 4, 2, 1, 9

The first pairwise comparison produces:

1, 3, 2, 5, 4, 2, 1, 9

The second pairwise comparison produces:

– CS1001 Lecture 35 – 1
1, 2, 3, 5, 4, 2, 1, 9

Continuing to the end of the list, the result of the


first traversal of the list produces:

1, 2, 3, 4, 2, 1, 5, 9

• The new list is then traversed again with the same


pairwise comparisons, and any necessary swaps
performed. This process is repeated until there
are no further swaps necessary.

• The result of the second traversal of the list is:

1, 2, 3, 2, 1, 4, 5, 9

After five traversals we obtained the sorted list:

1, 1, 2, 2, 3, 4, 5, 9

• Each pass through the list results in list elements


“bubbling” up to the top.

– CS1001 Lecture 35 – 2
Bubble Sort
# This function creates a list of length random numbers
# between 5 and 100.
import random

def randList(lst, length):


for index in range(length):
num=random.randint(5,100)
lst.append(num)

# This function sorts the list argument in descending order


# using bubble sort
def bubbleSort(lst):
i = len(lst)-1
swaps = True
while i > 0 and swaps:
swaps = False
print("Traversal",len(lst)-i)
for j in range(i):
if lst[j] < lst[j+1]:
print("Swapping elements",j,"and",j+1)
lst[j],lst[j+1] = lst[j+1],lst[j]
swaps = True
print(lst)
i -= 1
print()

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

You might also like