Uploaded by Jessica Chen

Bubble Sort

advertisement
🧋
Bubble Sort
Bubble Sort
Definition
Inefficient version
Most Efficient version
Questions
Good Revision Materials
Got some paper flashcards too
Bubble Sort
https://youtu.be/lyZQPjUT5B4
Definition
Sorting items in an array in ascending order by doing lots of comparisons and swaps
Bubble Sort
1
Inefficient version
Just to understand the mechanics
#setting up array
a=[15,16,6,8,5]
n = len(a)
for i in range(n-1):
for j in range(n-1):
if a[j]> a[j+1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
#end if
#end for
#end for
print(a)
#instead of doing temp =
a[j]; a[j] = a[j+1]; a[j+1] = temp, you could do:
#a[j],a[j+1] = a[j+1],a[j]
💡
Things to remember about this bubble sort
The outer loop to deal with passes
Inner loop to deal with comparisons
After the first pass the biggest number should be on the right hand side
If not specified, assume the sort is always in the in ascending order
Even once the array is sorted, the code will continue to sort through/make
comparisons in the array during the last pass - quite inefficient
Even if the array is ordered after 1 pass, it will still continue to sort through
n-1 passes. Imagine having n= 100
Most Efficient version
#setting up array
a=[15,16,6,8,5]
Bubble Sort
2
n = len(a)
for i in range(n-1):
flag=0
#boolean flag = false
for j in range(n-1-i):
#(n-j-i) number of comparisons reduced by i with each iteration because after each pass,
#the items that have been sorted to their correct position don't need to be compared again
if a[j]> a[j+1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
#if this code has been executed, then swapping has occured and the array is unordered
#flag = true and the loop will continue
flag = 1
#end if
#end for
if flag ==0:
# if flag remains false, then the swapping code hasn't been carried out
#it means that the array is sorted and the loop is broken
break
#end if
#end for
print(a)
💡
Tips
Some languages start indexes from 1 instead of 0, watch out in SQA,
otherwise marks will be lost
If not specified, use the efficient bubble sort
Questions
How does a bubble sort operate?
SQA 2008 MI
compares adjacent items[1]
if out of order swaps them[1]
Bubble Sort
3
repeats until there are no more swaps[1]
This is what I wrote
“
Starting from index 0, the bubble sort compares 2 items in adjacent indexes to
each other and swaps them if a preset condition is met
This comparison repeats until the desired number is in the correct position
This process repeats until there are no more swaps
What does the outer loop of a bubble sort deal with?
The number of passes
What does the inner loop of a bubble sort deal with?
The number of comparisons
If not specified, what order should the bubble sort resort to?
Ascending Order
Why is the bubble loop with 2 fixed loops inefficient?
Even once the array is sorted, the code will continue to sort through/make
comparisons in the array during the last pass
Disadvantage of Bubble Sort?
Used quadratic time, if the list increases, then the time taken to execute will increase
a ton
How to make Bubble Sort more efficient?
Use a Boolean variable
The space complexity for a bubble sort is O(1), because only a single additional memory
space is required i.e. for the temp variable
Bubble Sort
4
💡
Bubble Sort Summary
Definition: The process of sorting an unordered list by comparing adjacent
values, swapping if out of order and repeating until there are no more swaps
Good Revision Materials
Got some paper flashcards too
https://quizlet.com/gb/747674545/bubble-sort-flash-cards/?new
Bubble sort - Introducing algorithms - GCSE Computer Science Revision - BBC Bitesize
An example of a computer is bubble sort . This is a simple algorithm used for taking a list of
jumbled up numbers and putting them into the correct order. The algorithm runs as follows: Look at
the first number in the list. Compare the current number with the next number.
https://www.bbc.co.uk/bitesize/guides/z22wwmn/revision/3
AH Computing Revision - Bubble Sort
What is a Bubble Sort?
https://sites.google.com/rgc.aberdeen.sch.uk/rgcahcomputingrevi
sion/software-design/standard-algorithms/bubble-sort
http://www.bbc.co.uk/bitesize/guides/z22wwmn/revision/3
AH Computing Revision - Bubble Sort
What is a Bubble Sort?
http://sites.google.com/rgc.aberdeen.sch.uk/rgcahcomputingrevisi
on/software-design/standard-algorithms/bubble-sort
Bubble Sort
5
Run
n
1
0
j
2
Bubble Sort
6
Related documents
Download