By the end of this chapter, you will be able to:
Mastering these skills will help you organize data efficiently, which is essential for solving real-world problems in programming and technology.
Sorting is the process of arranging the items in a collection into a defined order, according to a rule that can be applied to every pair of items. The rule may place numbers from smallest to largest, names alphabetically, or dates from earliest to latest. Example: a SACCO keeps the records of its members in a list, and arranging them by member number lets the clerk find any member quickly instead of reading every record from the beginning.
In computing, sorting is carried out on data held in structures such as arrays and lists. A sorting algorithm is a finite, step-by-step set of instructions that compares items and rearranges them until the whole collection satisfies the chosen order.
Every sort has a direction. Ascending order places the smallest or earliest item first and the largest or latest item last, while descending order does the opposite. The direction is chosen according to the need: a list of fee arrears may be most useful with the largest balance first, whereas a register is most useful in alphabetical order.
Example: the numbers 45, 12, 78, 23 are arranged as follows.
Ascending: 12, 23, 45, 78
Descending: 78, 45, 23, 12
| Data type | Ascending order | Descending order |
|---|---|---|
| Numbers | 5, 9, 14, 30 | 30, 14, 9, 5 |
| Text | Achieng, Kamau, Wanjiru | Wanjiru, Kamau, Achieng |
| Dates | 02/01/2024, 15/03/2024, 30/09/2024 | 30/09/2024, 15/03/2024, 02/01/2024 |
| Money | Ksh 500, Ksh 2,000, Ksh 12,500 | Ksh 12,500, Ksh 2,000, Ksh 500 |
A few terms appear in every discussion of sorting. Understanding them makes it easier to read and write sorting algorithms.
Example: a secondary school stores each student as a record containing the admission number, the name and the mean mark. Sorting the records by mean mark produces a ranking, while sorting by name produces an alphabetical class list from the same data.
Sorted data is easier for people to read and much faster for computers to search. Many everyday systems depend on sorted data, and sorting is one of the most frequently performed operations in programming.
Sorting rules depend on the type of data being compared. A programmer must choose a rule that matches the data, otherwise the result may look correct to the computer but wrong to the user.
Example: a supermarket sorts its stock list by unit price in Ksh, then by product name, so that items with the same price appear in alphabetical order.
A sorting algorithm is correct only when its output satisfies two conditions. First, the items must be in the required order, so that every item comes before or equal to the item that follows it in the chosen direction. Second, the output must contain exactly the same items as the input, with none lost, added or changed. For example, if a list of five loan amounts is sorted, the result must still contain those same five amounts, now arranged from smallest to largest.
Create a free account to open more of this chapter.
Free: practical guides, quick cards, workplace scenarios and more.
Create a free accountType: Individual
| Tools & Equipment | Materials |
|---|---|
| Working computer | Plain papers |
| C++ IDE |
| S/N | Item | Quantity |
|---|---|---|
| 1 | Working computer | 1 Pc per Candidate |
| 2 | C++ IDE (e.g., Code::Blocks, Visual Studio, Dev-C++) | 1 Pc per Candidate |
| 3 | Plain papers | 2 Pcs per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Implement Bubble Sort | |||
| Observed computer laboratory rules (Award 2 marks or zero) | 2 | ||
| Launched C++ IDE successfully (Award 2 marks or zero) | 2 | ||
| Declared an integer array of size 15 (Award 2 marks or zero) | 2 | ||
| Captured input of 15 integers into the array (Award 1 mark for each 5 inputs correctly captured or zero) | 3 | ||
| Implemented Bubble Sort algorithm correctly with nested loops (Award 4 marks for outer loop, 4 marks for inner loop and swap logic) | 8 | ||
| Swapped elements correctly to sort in ascending order (Award 5 marks for correct swap implementation or zero) | 5 | ||
| Displayed the sorted array output correctly (Award 3 marks or zero) | 3 | ||
| Compiled, debugged, and executed the program without errors (Award 5 marks or zero) | 5 | ||
| Commented code appropriately for readability (Award 1 mark for each relevant comment or zero) | 2 | ||
| Sub-Total | 32 | ||
| PRODUCT CHECKLIST | |||
| Program sorts the array of 15 integers correctly in ascending order (Award 10 marks for correct and complete sorting output or zero) | 10 | ||
| Output is formatted neatly and clearly displayed on screen (Award 3 marks or zero) | 3 | ||
| Sub-Total | 13 | ||
| GRAND TOTAL | 45 | ||
Type: Individual
| Tools & Equipment | Materials |
|---|---|
| A working computer | Two plain papers |
| C++ IDE |
| S/N | Item | Quantity |
|---|---|---|
| 1 | A working computer | 1 Pc per Candidate |
| 2 | C++ IDE (e.g. Code::Blocks or Visual Studio) | 1 Pc per Candidate |
| 3 | Two plain papers | 2 Pcs per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Implement Selection Sort | |||
| Observed computer laboratory rules (Award 2 marks or zero) | 2 | ||
| Launched C++ IDE correctly (Award 1 mark or zero) | 1 | ||
| Declared an integer array of size 15 (Award 2 marks or zero) | 2 | ||
| Captured 15 integer elements into the array (Award 1 mark for each 5 elements correctly captured) | 3 | ||
| Implemented the Selection Sort algorithm correctly (Award marks based on correct implementation of selection, swapping, and iteration) | 10 | ||
| Displayed the sorted array output correctly (Award 5 marks for correct and clear display of sorted elements) | 5 | ||
| Commented code appropriately for readability (Award 1 mark for each relevant comment, max 3) | 3 | ||
| Compiled, debugged, and executed the program without errors (Award 4 marks or zero) | 4 | ||
| Sub-Total | 30 | ||
| PRODUCT CHECKLIST | |||
| Sorted array output is correct and matches ascending order for the 15 integers (Award 10 marks for correct sorted output matching Selection Sort results) | 10 | ||
| Code structure is well organized with proper indentation and naming conventions (Award 5 marks for neat and professional code layout) | 5 | ||
| Program runs to completion without runtime or logical errors (Award 5 marks for error-free execution) | 5 | ||
| Sub-Total | 20 | ||
| GRAND TOTAL | 50 | ||
At the start of this chapter we promised you would be able to:
Tick each one you can genuinely do.
So, are you there yet?
You're competent when you can confidently do 50% or more of what this chapter promised.
Sign in to record how you're doing.