Worksheet 10.1 Classical Algorithms Searching and Sorting.
Worksheet 10.1 Classical Algorithms Searching and Sorting.
1
Classical algorithms: searching and sorting.
Worksheet 10.1
1. Search………………. Antonym
2. Sort………………….. Abandon disorder none
3. Each………………… Later whole output
4. Until……………….. lose disorganized length
5. Input……………….
6. Find…………………
7. Ordered…………………….
8. Height………………………
9. Middle………………………….
Worksheet 10.3
Read the text, and each time you see a *stop* sign, you need to stop and think whether
you understand what you are reading or not. If not, you have to go back and reread.
If you want to become a software engineer, the most basic thing that you have to learn
are algorithms and data structures. The more algorithms and data structures you learn,
the more useful they will be in your career as a software engineer. To start, let’s learn
Search and Sort, two classes of algorithms a programmer can’t live without. *stop*
Searching
There are two categories of search algorithms we will study: linear and binary.
Linear search
Linear search algorithms means that the program will look at each item in the line (=input)
until it finds the necessary item. *stop* If you have 100 items and you need to search for
one specific item, then you have to look at every item in the input before you find the
necessary item. Linear = simple. *stop* For example: imagine you want to find your friend
Maria in a line of people standing in no particular order. You already know Maria´s
appearance, so you have to look at each person, one by one, in sequence, until you
recognize Maria. In doing so, you follow the linear search algorithm. *stop*
Binary search
Binary search (binary - “relating to 2 things”) works by dividing the input into two parts
until it finds the necessary item. *stop* One part contains the necessary item and the
other part does not. It is faster than linear search, but it only works with ordered
sequences – and this is very important, because the linear search does not need an
ordered sequence. *stop* For example: imagine you’re want to find your friend John (who
is 170 cm tall) in a line of people ordered by height from left to right, shortest to tallest. It
is a very long line, and you do not have time to go one-by-one like with the linear search.
What can you do? Use binary search. You select the person in the middle of the line, and
measure their height. The person is 165 cm tall. You immediately know that this person,
and all the people on their left, are not John. *stop* Next, you turn your attention to the
people on the right and select the middle person again. The person is 172 cm tall. You can
eliminate that person and all the people on the right. And so on, until you find the person
who is 170 cm tall – and that is John. In doing so, you follow the binary search algorithm.
*stop*
Sorting
MergeSort
Imagine you have an unordered group of people, and you need to order them by height.
First, you divide the group in two; then you divide each of the two groups in two again,
and so on – until you have individuals. *stop* Second, you put individuals in pairs; you put
the taller person to the right, until you organize all the pairs. Next, you put pairs in groups
of four, and order them. *stop*After that, you put the groups of four into groups of eight.
And so on, until you have a complete line of people ordered by height. By doing so, you
follow the MergeSort algorithm. *stop*
Worksheet 10.4
Read the following situations and figure out which of the three algorithms you have to
use in each situation: linear search, binary search or MergeSort.
1. There are 30 students in class. You need them to stand in a line according to their age,
from the youngest to the oldest, from right to left.
2. You have 15 circles that are ordered in the line from the smallest (2 cm diameter) to
the biggest (50 cm diameter). You need to find the circle that is 22 cm in diameter.
3. You are in a supermarket, and your mother asked you to buy coffee that is called
“Super Delicious Coffee”.
4. You are in the library, in the section of “Original English Literature”. There are 100
books that are ordered alphabetically. You need to find “Harry Potter and the Order of
Phoenix”.
5.
There are 10 people in a bank. The oldest people have a priority. You need to make a
line of people taking into account the priority: oldest person first, youngest person
last.
Worksheet 10.5