Skip to content
Exit presentation

Data Handling II: Databases & Structured Data

The answer, in order

Sorting the answer means moving whole rows. To sort is to put rows in order by one column. Here the order is smallest due first. Arrival order is the order in which the rows came.

Four rows, in arrival order.
Step 1 of 4
  1. Four rows, in arrival order.
  2. Smallest due found: A-107 with 0.
  3. Next smallest: A-104 with 800.
  4. The rest follow: whole rows moved.

The rule

Sorting moves whole rows and compares one field at the key's position. The comparison is strict. Tied rows are never swapped with each other. Sorting nothing is not an error.

The sort key is the column to sort by, such as due. The sign < is strict: equal dues do not count as smaller.

A pass is one trip through the rows to find the smallest. Two names hold positions, not rows. The name best marks the smallest due so far. The name look marks the row being checked. The name column is the position of the column to compare.

  1. if (answer[look][column] < answer[best][column]) {
  2. best = look;
  3. }

A swap exchanges two WHOLE rows. Their four values stay together, not just the due.

A-103, A-106, A-108 and A-111 all owe 1450 rupees. Sort only these four, and none is smaller than another. So none would move. But a swap with a smaller row can move a tied row. One tied row can end up after another it started before.

The lab has a copy loop. It copies the rows into a new list. You write the passes.

Loading lab…

Sorting nothing hands back nothing — that is not an error.

Jump to lesson

This track

  1. 0 Start here
  2. 1 One record, one row
  3. 2 The naming value
  4. 3 Naming the columns
  5. 4 Asking the register one question
  6. 5 Two conditions, both true
  7. 6 The answer, in order
  8. 7 Counting the groups in the answer
  9. 8 What a database adds
  10. 9 The big picture
  11. 10 Track quiz

Other tracks