You are looking at lesson 6. Your place is still lesson 9, task 1: Strings.
Linear and binary search, how many checks each needs, and selection sort.
You often need to find something in an array: whether it was −5 °C, where the grade 9 is. There are two ways, and they differ in how much work they take.
Check the boxes in turn until you find it. When found, return the index; if you checked them all and didn't find it, return -1 (no index is ever -1).
find(t, n, x):
for each i from 0 to n - 1:
if t[i] == x: return i
return -1
It works on any array. In the worst case (the value isn't there, or it is last) all n boxes are checked.
If the array is sorted, there is a cleverer way, like looking up a word in a dictionary. Look at the middle: if it holds less than you want, the left half is no longer needed; if more, the right half. Every check throws away half of the boxes left.
We look for 23 in 2 5 8 12 16 23 38 56 72 91 (indexes 0–9), with left, right and middle:
| Step | left | right | middle | t[middle] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 9 | 4 | 16 | 16 < 23 → left = 5 |
| 2 | 5 | 9 | 7 | 56 | 56 > 23 → right = 6 |
| 3 | 5 | 6 | 5 | 23 | found, index 5 |
Three checks instead of six. If left becomes greater than right, the value isn't there.
On an unsorted array binary search doesn't work: it throws away the half where the value actually was, and returns -1.
The largest number of checks:
| Boxes n | Linear | Binary |
|---|---|---|
| 10 | 10 | 4 |
| 1,000 | 1,000 | 10 |
| 1,000,000 | 1,000,000 | 20 |
The work of linear search grows with n: twice the data, twice the work. Binary search adds just one step each time n doubles (log₂ n). In the algorithms course you will write this as O(n) and O(log n). Here it is enough to be able to count how many times a loop runs.
To use binary search, the array has to be sorted. The simplest way is selection: find the smallest and swap it with the first, then the smallest of the rest with the second, and so on.
sort(t, n):
for each i from 0 to n - 2:
min = the index of the smallest of t[i]..t[n - 1]
swap t[i] and t[min]
5 4 1 3:
| i | Smallest of the rest | After the swap |
|---|---|---|
| 0 | 1 | 1 4 5 3 |
| 1 | 3 | 1 3 5 4 |
| 2 | 4 | 1 3 4 5 |
Comparisons: 3 + 2 + 1 = 6. For a thousand boxes, 999 + 998 + … + 1 = 499,500. Sorting is expensive, so it is worth it only when you will search many times afterwards. Faster ways to sort are in the Algorithms and Data Structures course.
Swapping two values takes a third variable (temp):
int temp = t[i];
t[i] = t[min];
t[min] = temp;
Without it, t[i] = t[min]; t[min] = t[i]; would give both boxes the same value.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.