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.
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller TR19. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $6.06. You're not tied to anything after your purchase.