Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

Rob-Bot Resources

A Level Computer Science

Sorting Algorithms
Learning Objectives
• To understand different methods of sorting data within an
array:
• Bubble sort
• Merge sort
• Explain how both methods work.
• To identify advantages and disadvantages of using both
techniques.
In order to do this, you will be asked to complete a range of tasks
including applying the sorts within Python.
Why do we Need to Sort Data?
• Tidying up, whether it be your bedroom, the kitchen or the files on your computer,
makes it easier to find things.
• The same applies within computer science. As we learned in the previous unit,
searching algorithms, it takes a lot longer to search large amounts of unsorted data
than it does to search through sorted data.
• Within computer science, sorting refers to the process of placing a collection of
data into an order. For example, alphabetically or ascending or descending
numerical size.
• Sorting large amounts of data can take a substantial amount of computing
resources. As such, improving the efficiency of sorting algorithms is a popular area
of study within computer science. Like searching, the efficiency of a sorting
algorithm is related to the number of items being processed.
• Within this unit, we will be looking at two different types of sorting algorithms:
• Bubble sort algorithm
• Merge sort algorithm
What is a Bubble Sort?
A Bubble sort algorithm is named as such because the numbers are said to ‘bubble’ into the
correct position!
It works as follows:
1. The first two items (e.g. index 0 and 1) within an array are compared.
2. If they are not in the required order, their positions in the array are swopped round.
3. The next two items (e.g. 1 and 2) are then compared. This process continues until the
algorithm gets to the end of the list. This is known as a pass.
4. Passes are repeated until there are no more swaps to make.

The animation below shows you how it works:

Animation
Bubble sort the array below into ascending order:
Understanding a Bubble Sort Array index 0 1 2 3 4 5 6 7 8 9
Array data 20 12 44 23 75 81 62 5 13 54
• When you take a closer look, a First Pass
bubble sort algorithm compares
two adjoining array index [0] v [1] 20 12 44 23 75 81 62 5 13 54
positions at a time. [1] v [2] 12 20 44 23 75 81 62 5 13 54
• As you can see in the graphic to
[2] v [3] 12 20 44 23 75 81 62 5 13 54
the right, if the numbers are not
in the correct order, their [3] v [4] 12 20 23 44 75 81 62 5 13 54
positions are swopped, otherwise 12 20 23 44 75 81 62 5 13 54
[4] v [5]
they remain the same.
• If data is still unsorted and the [5] v [6] 12 20 23 44 75 81 62 5 13 54
comparisons reach the end of the [6] v [7] 12 20 23 44 75 62 81 5 13 54
array, the process is restarted.
[7] v [8] 12 20 23 44 75 62 5 81 13 54
• This process is continued until all
data is sorted. [8] v [9] 12 20 23 44 75 62 5 13 81 54

Array after 1st pass: 12 20 23 44 75 62 5 13 54 81


Bubble Sort Algorithm
Task: The algorithm below will sort out information in a given array into
Assume array indexing begins at 0 ascending order. Place the explanations below into the correct locations.
SUBROUTINE bubbleSort (array) Pseudocode Explanation

swap  TRUE
swap  TRUE
WHILE swap = TRUE WHILE swap = TRUE

swap  FALSE
FOR i  1 TO LEN(array)-1
swap  FALSE
IF array [i-1] > array [i]
temp  array [i]
array [i]  array [i-1]
FOR i  1 TO LEN(array)-1
array [i-1]  temp
swap  TRUE
IF array [i-1] > array [i]
END IF temp  array [i]
array [i]  array [i-1]
END FOR array [i-1]  temp
END WHILE
swap  TRUE
END SUBROUTINE
Bubble Sort Algorithm
Task: The algorithm below will sort out information in a given array into
Assume array indexing begins at 0 ascending order. Place the explanations below into the correct locations.
SUBROUTINE bubbleSort (array) Pseudocode Explanation

swap  TRUE
swap  TRUE
WHILE swap = TRUE WHILE swap = TRUE

swap  FALSE
FOR i  1 TO LEN(array)-1
swap  FALSE
IF array [i-1] > array [i]
temp  array [i]
array [i]  array [i-1]
FOR i  1 TO LEN(array)-1
array [i-1]  temp
swap  TRUE
IF array [i-1] > array [i]
END IF temp  array [i]
array [i]  array [i-1]
END FOR array [i-1]  temp
END WHILE
swap  TRUE
END SUBROUTINE
Task: Understanding Bubble Sort
5 3 1 2 4 Swap? (Yes/No)
Bubble sort the following array into ascending order. 5 3 1 2 4
Show each step of the sort within the table. Start
Note: the compared data items have been
identified for you.

1 2 3 4 5 End
Task: Understanding Bubble Sort
5 3 1 2 4 Swap? (Yes/No)
Bubble sort the following array into ascending order. 5 3 1 2 4 Yes
Show each step of the sort within the table. Start 3 5 1 2 4 Yes
Note: the compared data items have been 3 1 5 2 4 Yes
identified for you. 3 1 2 5 4 Yes
3 1 2 4 5 Yes
1 3 2 4 5 No
1 3 2 4 5 No
1 3 2 4 5 No
1 3 2 4 5 Yes
1 2 3 4 5 No
1 2 3 4 5 No
1 2 3 4 5 No
1 2 3 4 5 No
1 2 3 4 5 No
1 2 3 4 5 No
1 2 3 4 5 End
Writing a Bubble Sort Algorithm in Python
• The following procedure will run an
ascending bubble sort on an
inputted array.
• Note how the FOR loop uses a
range.
• This allows you to determine a
number at which indexing starts
from (1) and a number which, when
reached, the loop stops (in this case,
the length of the array).
• To apply the bubbleSort procedure,
the following code will output a
‘before and after’ of the sort.
Task: Create a Python Bubble Sort

• Create a Python bubble sort on the following array:

examScores  [92,55,84,48,72,90,87,54,67,75]

• Output both the original and sorted arrays.


• Present your code and output data on the next slide.
Print Screen Your Code Evidence
Explain your code! Top Tip: Check your code works! Print out
Pro/Beast: Include comments (#) within your array at each pass or data exchange
your programming to explain your code! so you can check its working as expected!
Add a print screen to show your coding here. Add a print screen to show your output here.
Making the Bubble Sort More Efficient
Original Algorithm
• A bubble sort is often
considered a thorough, but
inefficient sorting method.
• Our current code only stops
checking if data needs
swapping if it has no Animation

exchanges on a full pass of all


the data. Improved Algorithm
• However, we can adapt the
code so that data that we
know has been sorted is not
repeatedly checked as shown
here: Animation
Pro/Beast Task: Improve the
Efficiency of the Algorithm SUBROUTINE bubbleSort (array)
swap  TRUE
WHILE swap = TRUE
• Part 1. Edit the pseudocode below so that swap  FALSE
sorted data is not repeatedly checked. FOR i  1 TO LEN(array)-1
• Annotate the changes made. IF array [i-1] > array [i]
• Explain how you plan to improve your temp  array [i]
code in the box below. array [i]  array [i-1]
How you will improve the code: array [i-1]  temp
swap  TRUE
END IF
END FOR
END WHILE
END SUBROUTINE
Pro/Beast Task: Improve SUBROUTINE bubbleSort (array)
swap  TRUE
the Efficiency of the end  LEN(array)

Algorithm Added variable to


WHILE swap = TRUE
swap  FALSE
• Part 1. Edit the pseudocode store the length of theAdd a print screen to show your code here.
below
array.
so that FOR i  1 TO end-1
sorted data is not repeatedly checked. IF array [i-1] > array [i]
The variable is used
• Annotate the changes made. within the FOR loop. temp  array [i]
• Explain how you plan to improve your
After each pass, the array [i]  array [i-1]
code in the box below. last value compared in
the array is sorted,
array [i-1]  temp
How you will improve the code: therefore we know it swap  TRUE
doesn’t need END IF
comparing again,
therefore, after each END FOR
pass, end (which end  end-1
originally holds the
END WHILE
length of the whole
array) is reduced by 1. END SUBROUTINE
Pro/Beast Task: Improve the Efficiency of the Algorithm

• Part 2. Improve your Python Add a print screen to show your code here.

code by implementing the


changes made to your
pseudocode.
• Print screen your improved
code below.
• Beast Challenge: Adapt the
code so that a user can input
their own scores.
Pro/Beast Task: Improve the Efficiency of the Algorithm

• Part 2. Improve your Python Add a print screen to show your code here.

code by implementing the


changes made to your
pseudocode.
• Print screen your improved
code below.
• Beast Challenge: Adapt the
code so that a user can input
their own scores.
Task: Understanding Improved Bubble Sort
Use the improved bubble sort code below to sort the
data in the array.

5 3 1 2 4 Swap? (Yes/No)
5 3 1 2 4
Start

1 2 3 4 5 End
Task: Understanding Improved Bubble Sort
Use the improved bubble sort code below to sort the
data in the array.

5 3 1 2 4 Swap? (Yes/No)
5 3 1 2 4 Yes
Start 3 5 1 2 4 Yes
3 1 5 2 4 Yes
3 1 2 5 4 Yes
3 1 2 4 5 Yes
1 3 2 4 5 Yes
1 2 3 4 5 No
1 2 3 4 5 No
1 2 3 4 5 No
1 2 3 4 5 No
1 2 3 4 5 End
Bubble Sort algorithm Review
Question Answer

1. Explain why the bubble sort technique could be


regarded as a robust sorting technique?

2. Explain why the bubble sort technique is regarded


as an inefficient method of sorting arrays?

3. Suppose you have the following list of numbers to sort: [19, 1, 9, 7, 3, 10, 13, 15, 8, 12]
Which list represents the partially sorted list after three complete passes of bubble sort (Highlight the correct
answer in RED)?
(A) [1, 9, 19, 7, 3, 10, 13, 15, 8, 12]
(B) [1, 3, 7, 9, 10, 8, 12, 13, 15, 19]
(C) [1, 7, 3, 9, 10, 13, 8, 12, 15, 19]
(D) [1, 9, 19, 7, 3, 10, 13, 15, 8, 12]
Bubble Sort Algorithm Review
Question Answer
Each individual data item is compared in turn.
1. Explain why the bubble sort technique could be The algorithm will only stop once it knows all
regarded as a robust sorting technique? data is placed in the correct order.

Data that has already been sorted may be


rechecked by the algorithm resulting in wasted
2. Explain why the bubble sort technique is regarded time. It may be a very slow process when
as an inefficient method of sorting arrays?
checking large arrays.

3. Suppose you have the following list of numbers to sort: [19, 1, 9, 7, 3, 10, 13, 15, 8, 12]
Which list represents the partially sorted list after three complete passes of bubble sort (Highlight the correct
answer in RED)?
(A) [1, 9, 19, 7, 3, 10, 13, 15, 8, 12]
(B) [1, 3, 7, 9, 10, 8, 12, 13, 15, 19]
(C) [1, 7, 3, 9, 10, 13, 8, 12, 15, 19]
(D) [1, 9, 19, 7, 3, 10, 13, 15, 8, 12]
What is a Merge Sort?
The algorithm below will sort out information in a given array into
ascending order.

• Merging is the process of taking two smaller


sorted arrays and combining them together
into a single, sorted, new array.
• For this to happen, the original array is
recursively split into two smaller arrays until
each array only holds a single item.
• Then, each array is merged back with another
array until the original array is reassembled
and data is sorted into the required order.
• It is often referred to as a “Divide and
Conquer” strategy!
• The animation to the right shows how the
process works:

Animation
Understanding a Merge Sort
• As the previous animation
showed, there is a clear pattern
of behavior in how a merge sort
works.
• The algorithm focuses on
dividing (or splitting) one part of
an array before merging begins.
• It then moves onto the next
array, first splitting, then
merging until the whole array is
reformed within a sorted order.
Merge Sort Algorithm Task: The algorithm below will sort out information in a given array into ascending order.
Place the explanations below into the correct locations.
Part 1: Splitting Pseudocode Explanation
SUBROUTINE mergeSort (array)
IF LEN(array) >1
mid  LEN(array) DIV 2
left  array[0 TO mid]
right  FOR i  array[mid+1 TO LEN(array)]
mergeSort(left)
IF LEN(array) > 1
mergeSort(right)
a0 mid  array DIV 2
b0 left  array[0 TO mid]
c0 right  array[mid+1 TO LEN(array)]
WHILE a < LEN(left) AND b < LEN(right)
IF left[a] < right[b]
array[c]  left[a]
aa+1
ELSE
array[c]  right[b]
bb+1
END IF
cc+1
END WHILE
WHILE a < LEN(left) mergeSort(left)
array[c]  left[a] mergeSort(right)
aa+1
cc+1
END WHILE
WHILE b < LEN(right)
array[c]  right[b]
bb+1
cc+1
END WHILE
Note: DIV refers to integer division, meaning the result is rounded down to the
END IF nearest whole number, e.g. 9 DIV 2 = 4, 5 DIV 2 = 2, etc.
END SUBROUTINE
Merge Sort Algorithm Task: The algorithm below will sort out information in a given array into ascending order.
Place the explanations below into the correct locations.
Part 2: Merging Pseudocode Explanation
SUBROUTINE mergeSort (array)
IF LEN(array) >1 a0
mid  LEN(array) DIV 2 b0
left  array[0 TO mid]
c0
right  FOR i  array[mid+1 TO LEN(array)]
mergeSort(left)
mergeSort(right) WHILE a < LEN(left) AND b < LEN(right)
a0 IF left[a] < right[b]
b0 array[c]  left[a]
c0 aa+1
WHILE a < LEN(left) AND b < LEN(right) ELSE
IF left[a] < right[b] array[c]  right[b]
array[c]  left[a] bb+1
aa+1 END IF
ELSE cc+1
array[c]  right[b]
bb+1
END IF WHILE a < LEN(left)
cc+1 array[c]  left[a]
END WHILE aa+1
WHILE a < LEN(left) cc+1
array[c]  left[a]
aa+1
cc+1 WHILE b < LEN(right)
END WHILE array[c]  right[b]
WHILE b < LEN(right) bb+1
array[c]  right[b] cc+1
bb+1
cc+1
END WHILE
END IF
END SUBROUTINE Note: The term parent array refers to the array that was split to form the left and right arrays.
Merge Sort Algorithm Task: The algorithm below will sort out information in a given array into ascending order.
Place the explanations below into the correct locations.
Part 1: Splitting Pseudocode Explanation
SUBROUTINE mergeSort (array)
IF LEN(array) >1
mid  LEN(array) DIV 2
left  array[0 TO mid]
right  FOR i  array[mid+1 TO LEN(array)]
mergeSort(left)
IF LEN(array) > 1
mergeSort(right)
a0 mid  array DIV 2
b0 left  array[0 TO mid]
c0 right  array[mid+1 TO LEN(array)]
WHILE a < LEN(left) AND b < LEN(right)
IF left[a] < right[b]
array[c]  left[a]
aa+1
ELSE
array[c]  right[b]
bb+1
END IF
cc+1
END WHILE
WHILE a < LEN(left) mergeSort(left)
array[c]  left[a] mergeSort(right)
aa+1
cc+1
END WHILE
WHILE b < LEN(right)
array[c]  right[b]
bb+1
cc+1
END WHILE
Note: DIV refers to integer division, meaning the result is rounded down to the
END IF nearest whole number, e.g. 9 DIV 2 = 4, 5 DIV 2 = 2, etc.
END SUBROUTINE
Merge Sort Algorithm Task: The algorithm below will sort out information in a given array into ascending order.
Place the explanations below into the correct locations.
Part 2: Merging Pseudocode Explanation
SUBROUTINE mergeSort (array)
IF LEN(array) >1 a0
mid  LEN(array) DIV 2 b0
left  array[0 TO mid]
c0
right  FOR i  array[mid+1 TO LEN(array)]
mergeSort(left)
mergeSort(right) WHILE a < LEN(left) AND b < LEN(right)
a0 IF left[a] < right[b]
b0 array[c]  left[a]
c0 aa+1
WHILE a < LEN(left) AND b < LEN(right) ELSE
IF left[a] < right[b] array[c]  right[b]
array[c]  left[a] bb+1
aa+1 END IF
ELSE cc+1
array[c]  right[b]
bb+1
END IF
cc+1 WHILE a < LEN(left)
END WHILE array[c]  left[a]
WHILE a < LEN(left) aa+1
array[c]  left[a]
cc+1
aa+1
cc+1 WHILE b < LEN(right)
END WHILE
WHILE b < LEN(right)
array[c]  right[b]
array[c]  right[b] bb+1
bb+1 cc+1
cc+1
END WHILE
END IF
END SUBROUTINE Note: The term parent array refers to the array that was split to form the left and right arrays.
Understanding Merge Sort
Merge sort the following array into ascending order. Show each step of the sort within the table.

92 55 16 38 75 64 42 24
Understanding Merge Sort
Merge sort the following array into ascending order. Show each step of the sort within the table.

92 55 16 38 75 64 42 24

92 55 16 38 75 64 42 24

92 55 16 38 75 64 42 24

92 55 16 38 75 64 42 24

55 92 16 38 64 75 24 42

16 38 55 92 24 42 64 75

16 24 38 42 55 64 75 92
Beast: Understanding Merge Sort
Merge sort the following array into ascending order. Show each step of the sort within the table.
95 22 78 51 11 46 37 18 91 81
Beast: Understanding Merge Sort
Merge sort the following array into ascending order. Show each step of the sort within the table.
95 22 78 51 11 46 37 18 91 81

95 22 78 51 11 46 37 18 91 81

95 22 78 51 11 46 37 18 91 81

95 22 78 51 11 46 37 18 91 81

22 95 51 11 37 46 91 81

11 51 81 91

11 51 78 18 81 91

11 22 51 78 95 18 37 46 81 91

11 18 22 37 46 51 78 81 91 95
Writing a Merge Sort Algorithm in Python
In Python, an integer divide (DIV in pseudocode)
is represented using the symbols // .

The Python code array[:mid] creates an array


from all the elements up to and including the mid
index.

The code array[mid:] creates an array from all


elements after the mid index.

To apply and output the mergeSort procedure


onto an array, use the following code:
Writing a Merge Sort Algorithm in Python
By adding print
statements, you
can see how the
splitting and
merging process
takes place!
Task: Create a Python Merge Sort

• Create a Python merge sort on the following array:

examScores  [92,55,84,48,72,90,87,54,67,75]

• Output both the original and sorted arrays.


• Present your code and output data on the next slide.
Print Screen Your Code Evidence
Explain your code! Top Tip: Check your code works! Use print
Pro/Beast: Include comments (#) within to check splitting and merging is taking
your programming to explain your code! place as expected!
Add a print screen to show your coding here. Add a print screen to show your output here.
Merge Sort Algorithm Review
Question Answer

1. Explain why a programmer would prefer to use a merge sort over


a bubble sort.

2. Explain a disadvantage of a merge sort over a bubble sort.

3. Given the following list of numbers: [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] which answer shows the list to be sorted
after 3 recursive calls to merge sort (Highlight the correct answer in RED)?
(A) [16, 49, 39, 27, 43, 34, 46, 40]
(B) [21,1]
(C) [21, 1, 26, 45]
(D) [21]

4. Given the following list of numbers: [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] which answer shows the first two lists to be
merged (Highlight the correct answer in RED)?
(A) [21, 1] and [26, 45]
(B) [[1, 2, 9, 21, 26, 28, 29, 45] and [16, 27, 34, 39, 40, 43, 46, 49]
(C) [21] and [1]
(D) [9] and [16]
Merge Sort Algorithm Review
Question Answer
It is much more efficient and quicker to run a merge sort
1. Explain why a programmer would prefer to use a merge sort over on a large array in comparison to a bubble sort algorithm.
a bubble sort.

As it creates new arrays, a merge sort uses more memory


2. Explain a disadvantage of a merge sort over a bubble sort. than a bubble sort.

3. Given the following list of numbers: [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] which answer shows the list to be sorted
after 3 recursive calls to merge sort (Highlight the correct answer in RED)?
(A) [16, 49, 39, 27, 43, 34, 46, 40]
(B) [21,1]
(C) [21, 1, 26, 45]
(D) [21]

4. Given the following list of numbers: [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] which answer shows the first two lists to be
merged (Highlight the correct answer in RED)?
(A) [21, 1] and [26, 45]
(B) [[1, 2, 9, 21, 26, 28, 29, 45] and [16, 27, 34, 39, 40, 43, 46, 49]
(C) [21] and [1]
(D) [9] and [16]
Bubble vs Merge Sort Algorithms
Place the statements below into the correct locations within the table.
Algorithm Advantages Disadvantages

Bubble Sort

Merge Sort
Bubble vs Merge Sort Algorithms
Place the statements below into the correct locations within the table.
Algorithm Advantages Disadvantages

Bubble Sort

Merge Sort
Exam Style Questions
4. Complete the missing
1. In the space below, explain how a 2. You are conducting an ascending 3. In the space below, explain how a
numbers in the merge sort
bubble sort algorithm works: bubble sort on the array below. merge sort algorithm works:
diagrams below:
10 7 3 8 11 21 4 9 4(a) Ascending merge sort:

2(a). Write the order of the items


after the first pass.
9 4 7 2

2(b). Write the order of the items


after the second pass.

2(c). Write the order of the items 4(b) Descending merge sort:
after the third pass.
4 5 3 7 8 1

6
2(d). How many more passes would
be needed to sort the array? 8 6 2 1

Answer: 8 7 6 5 4 3 2 1
Exam Style Questions
4. Complete the missing
1. In the space below, explain how a 2. You are conducting an ascending 3. In the space below, explain how a
numbers in the merge sort
bubble sort algorithm works: bubble sort on the array below. merge sort algorithm works:
diagrams below:

• A Bubble sort algorithm is 10 7 3 8 11 21 4 9 • Merging is the process of 4(a) Ascending merge sort:
named as such because the taking two smaller sorted
numbers are said to ‘bubble’ 2(a). Write the order of the items arrays and combining them
after the first pass.
9 4 7 2
into the correct position. together into a single, sorted,
• The first two items (e.g. index 7 3 8 10 11 4 9 21 new array.
0 and 1) within an array are • For this to happen, the original 4 9 2 7
compared. 2(b). Write the order of the items array is recursively split into
• If they are not in the required after the second pass. two smaller arrays until each
order, their positions in the array only holds a single item. 2 4 7 9
3 7 8 10 4 9 11 21
array are swopped round. • Then, each array is merged
• The next two items (e.g. 1 and back with another array until
2(c). Write the order of the items 4(b) Descending merge sort:
2) are then compared. This after the third pass. the original array is
process continues until the reassembled and data is 4 5 3 7 2 8 6 1
algorithm gets to the end of 3 7 8 4 9 10 11 21 sorted into the required order.
the list. This is known as a 5 4 7 3 8 2 6 1

pass. 2(d). How many more passes would


• Passes are repeated until there be needed to sort the array? 3 4 5 7 8 6 2 1

are no more swaps to make.


Answer: 1 8 7 6 5 4 3 2 1
Review of Learning Objectives

After completing this unit of work, you should now be able to:

• Understand different methods of sorting data within an array:


• Bubble sort
• Merge sort
• Explain how both methods work.
• Identify advantages and disadvantages of using both
techniques.

You might also like