Algorithms: for, if and Sorting
3 Examples and 6 ClassWork tasks. Learn loops and sorting with diagrams.
Algorithms: for, if and Sorting(for、if 與排序)
What will you learn?(這一單元學甚麼?)
First, use for + if to find something in an array(陣列). Then use two for loops (a nested loop(雙層迴圈)) to sort(排序) the array.
All three Examples use the same data, so you can compare them:
35 52 68 12 47 52 36 52 74 27
Learning path(學習路線)
| Order / 次序 | Example | Structure / 結構 | ClassWork |
|---|---|---|---|
| 1 | Sequential Search(循序搜尋) | One for + if | 1-1 Pass count, 1-2 Maximum and its position |
| 2 | Selection Sort(選擇排序) | Two for + if + swap | 2-1 Largest to smallest, 2-2 Print every round |
| 3 | Bubble Sort(冒泡排序) | Two for + if + swap | 3-1 Count the swaps, 3-2 Stop early |
How to work through each Example(每個 Example 的做法)
- Real-life scene: Look at the picture and guess what happens next.
- Trace the diagram: Follow a few steps by hand(手算追蹤).
- Read the code: Match the code to the diagram. Find the for and the if.
- ClassWork: Do the two tasks. Test with the sample input; your output must match exactly.
- Learning notes: Write one note card with Excerpt and Comment(節錄評論法). Link the algorithm to your own experience.
Before you start(開始前檢查)
- You can write
for (int i = 0; i < n; i++) - You can write
if (...) { ... } - You know arrays start at index 0:
data[0]is the first number(陣列由第 0 格開始)