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 / 次序ExampleStructure / 結構ClassWork
1Sequential Search(循序搜尋)One for + if1-1 Pass count, 1-2 Maximum and its position
2Selection Sort(選擇排序)Two for + if + swap2-1 Largest to smallest, 2-2 Print every round
3Bubble Sort(冒泡排序)Two for + if + swap3-1 Count the swaps, 3-2 Stop early

How to work through each Example(每個 Example 的做法)

  1. Real-life scene: Look at the picture and guess what happens next.
  2. Trace the diagram: Follow a few steps by hand(手算追蹤).
  3. Read the code: Match the code to the diagram. Find the for and the if.
  4. ClassWork: Do the two tasks. Test with the sample input; your output must match exactly.
  5. 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 格開始)
Built with LogoFlowershow