Professional Documents
Culture Documents
SortingAlgorithms P16
SortingAlgorithms P16
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.
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
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
examScores [92,55,84,48,72,90,87,54,67,75]
• Part 2. Improve your Python Add a print screen to show your code here.
• Part 2. Improve your Python Add a print screen to show your code here.
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
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.
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.
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)
a0 mid array DIV 2
b0 left array[0 TO mid]
c0 right array[mid+1 TO LEN(array)]
WHILE a < LEN(left) AND b < LEN(right)
IF left[a] < right[b]
array[c] left[a]
aa+1
ELSE
array[c] right[b]
bb+1
END IF
cc+1
END WHILE
WHILE a < LEN(left) mergeSort(left)
array[c] left[a] mergeSort(right)
aa+1
cc+1
END WHILE
WHILE b < LEN(right)
array[c] right[b]
bb+1
cc+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 a0
mid LEN(array) DIV 2 b0
left array[0 TO mid]
c0
right FOR i array[mid+1 TO LEN(array)]
mergeSort(left)
mergeSort(right) WHILE a < LEN(left) AND b < LEN(right)
a0 IF left[a] < right[b]
b0 array[c] left[a]
c0 aa+1
WHILE a < LEN(left) AND b < LEN(right) ELSE
IF left[a] < right[b] array[c] right[b]
array[c] left[a] bb+1
aa+1 END IF
ELSE cc+1
array[c] right[b]
bb+1
END IF WHILE a < LEN(left)
cc+1 array[c] left[a]
END WHILE aa+1
WHILE a < LEN(left) cc+1
array[c] left[a]
aa+1
cc+1 WHILE b < LEN(right)
END WHILE array[c] right[b]
WHILE b < LEN(right) bb+1
array[c] right[b] cc+1
bb+1
cc+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)
a0 mid array DIV 2
b0 left array[0 TO mid]
c0 right array[mid+1 TO LEN(array)]
WHILE a < LEN(left) AND b < LEN(right)
IF left[a] < right[b]
array[c] left[a]
aa+1
ELSE
array[c] right[b]
bb+1
END IF
cc+1
END WHILE
WHILE a < LEN(left) mergeSort(left)
array[c] left[a] mergeSort(right)
aa+1
cc+1
END WHILE
WHILE b < LEN(right)
array[c] right[b]
bb+1
cc+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 a0
mid LEN(array) DIV 2 b0
left array[0 TO mid]
c0
right FOR i array[mid+1 TO LEN(array)]
mergeSort(left)
mergeSort(right) WHILE a < LEN(left) AND b < LEN(right)
a0 IF left[a] < right[b]
b0 array[c] left[a]
c0 aa+1
WHILE a < LEN(left) AND b < LEN(right) ELSE
IF left[a] < right[b] array[c] right[b]
array[c] left[a] bb+1
aa+1 END IF
ELSE cc+1
array[c] right[b]
bb+1
END IF
cc+1 WHILE a < LEN(left)
END WHILE array[c] left[a]
WHILE a < LEN(left) aa+1
array[c] left[a]
cc+1
aa+1
cc+1 WHILE b < LEN(right)
END WHILE
WHILE b < LEN(right)
array[c] right[b]
array[c] right[b] bb+1
bb+1 cc+1
cc+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 // .
examScores [92,55,84,48,72,90,87,54,67,75]
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.
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(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
After completing this unit of work, you should now be able to: