Computational Thinking – Assignment 4 (Artificial Intelligence Year 1)
1. How to use merge sort?
Keys E X A M P L E
Index 0 1 2 3 4 5 6
The “Tree” below shows the merge sort for the list { E X A M P L E }. We can see that this list
is recursively divided into two halves, until the size of a particular part of the list is 1. Once this
happened for every single letter in this list, the merge sort will start. This algorithm starts
merging elements back, until the whole list is merged, in this case in alphabetic order. The last
step is merging the last two sorted halves.
2. Sort by Python
A. We need to sort the coordinates in ascending order by comparing the first part of the
coordinates. The coordinates are (f, 9), (z, 2), (t, 4), (x, 8), (b, 1), (m, 7).
Input =
, Computational Thinking – Assignment 4 (Artificial Intelligence Year 1)
Output =
As you can see, the first part of the coordinates, the letters, are now in ascending (=
alphabetic) order.
B. We need to sort the coordinates in ascending order by comparing the second part of the
coordinates. The coordinates are (f, 9), (z, 2), (t, 4), (x, 8), (b, 1), (m, 7).
Input =
Output =
As you can see, the second part of the coordinates, the numbers, are now in ascending order.
3. Sort and search
I.
Keys T F W C Q G P R O
Index 0 1 2 3 4 5 6 7 8
Unsorted list
A. First, we have to select one element from the list (this can be any element): the pivot. Let’s say we
select P from the list. Now we rearrange this list in such a way that all the elements lesser than or
equal to the P are towards the left of this pivot and all the elements greater than or greater than or
equal to the P are towards the right of this pivot. The list will look like this:
II. Keys F C G O P T W Q R
Index 0 1 2 3 4 5 6 7 8
Partitioned list
We call this whole process the “partitioning” of the list. The pivot is now in its final position in the sorted
array. Once we have partitioned the array like this (II. Partitioned list) all the elements smaller than the
pivot are to the left and all the elements greater than the pivot are to the right. We can break this
problem into two sub-problems: subproblem 1 and subproblem 2.
1. How to use merge sort?
Keys E X A M P L E
Index 0 1 2 3 4 5 6
The “Tree” below shows the merge sort for the list { E X A M P L E }. We can see that this list
is recursively divided into two halves, until the size of a particular part of the list is 1. Once this
happened for every single letter in this list, the merge sort will start. This algorithm starts
merging elements back, until the whole list is merged, in this case in alphabetic order. The last
step is merging the last two sorted halves.
2. Sort by Python
A. We need to sort the coordinates in ascending order by comparing the first part of the
coordinates. The coordinates are (f, 9), (z, 2), (t, 4), (x, 8), (b, 1), (m, 7).
Input =
, Computational Thinking – Assignment 4 (Artificial Intelligence Year 1)
Output =
As you can see, the first part of the coordinates, the letters, are now in ascending (=
alphabetic) order.
B. We need to sort the coordinates in ascending order by comparing the second part of the
coordinates. The coordinates are (f, 9), (z, 2), (t, 4), (x, 8), (b, 1), (m, 7).
Input =
Output =
As you can see, the second part of the coordinates, the numbers, are now in ascending order.
3. Sort and search
I.
Keys T F W C Q G P R O
Index 0 1 2 3 4 5 6 7 8
Unsorted list
A. First, we have to select one element from the list (this can be any element): the pivot. Let’s say we
select P from the list. Now we rearrange this list in such a way that all the elements lesser than or
equal to the P are towards the left of this pivot and all the elements greater than or greater than or
equal to the P are towards the right of this pivot. The list will look like this:
II. Keys F C G O P T W Q R
Index 0 1 2 3 4 5 6 7 8
Partitioned list
We call this whole process the “partitioning” of the list. The pivot is now in its final position in the sorted
array. Once we have partitioned the array like this (II. Partitioned list) all the elements smaller than the
pivot are to the left and all the elements greater than the pivot are to the right. We can break this
problem into two sub-problems: subproblem 1 and subproblem 2.