Menu
Theme
Bachelor of Science in Computer Science
Course Content

Sorting/Searching

Data Structures & Algorithms

Habari! Let's Tame the Data Chaos: Sorting & Searching!

Hello there, future tech guru! Welcome. Imagine you walk into the Kenya National Library to find one specific book, "The River and the Source." Now, picture two scenarios:

  1. The library is a total mess. Books are everywhere, no order, no system. You'd probably spend the whole day, maybe even a week, just looking!
  2. The library is perfectly organised. Books are arranged by genre, then alphabetically by author. You can find your book in minutes.

That, my friend, is the power of sorting and searching! In the world of computers, data is like those books. Without order, finding anything is a nightmare. Today, we'll learn the magic tricks programmers use to bring order to this chaos and find information faster than you can say "Sawa sawa!"


Part 1: The Art of Searching - Finding a Needle in a Digital Haystack

Searching is exactly what it sounds like: looking for a specific piece of data within a larger collection. Think about searching for a friend's contact on your phone or finding an M-Pesa transaction from last week.

A. Linear Search: The "Moja kwa Moja" Approach

This is the simplest search method. You start at the beginning of a list and check each item, one by one, until you find what you're looking for or reach the end. It’s like looking for your size of shoe in a pile at Gikomba market – you pick one, check it, put it down, pick the next one...

Real-World Scenario: A conductor on a matatu needs to check if a passenger named 'Wanjiku' has paid. He has a paper list of names. He starts from the first name, reads it, moves to the second, the third, and so on, until he finds 'Wanjiku'. That is a linear search!

// Let's find the number 15 in this list of counties by their code
County Codes: [47, 1, 30, 15, 22]

Step 1: Is the first item (47) == 15? No.
Step 2: Is the second item (1) == 15? No.
Step 3: Is the third item (30) == 15? No.
Step 4: Is the fourth item (15) == 15? Yes! Found it at position 4.

-- ASCII Diagram --

List: [47] -> [ 1] -> [30] -> [15] -> [22]
        ^       ^       ^       ^
        |       |       |       |
      Check 1 Check 2 Check 3 Check 4 (Found!)
  • Good for: Small or unsorted lists.
  • The Catch: Imagine searching for the last person on a list of 47 million Kenyans. It would be incredibly slow!
  • Efficiency (Big O Notation): We call its performance O(n). This means in the worst case, if the item is at the end or not there at all, you have to look through all 'n' items.

B. Binary Search: The "Divide and Conquer" Strategy

This one is much smarter, but it has one very important rule: the list must be sorted first!

Instead of starting at the beginning, you start in the middle. You check if the middle item is what you want. If not, you check if your item is bigger or smaller. If it's smaller, you can ignore the entire second half of the list! If it's bigger, you ignore the first half. You keep cutting the list in half until you find your item.

Real-World Scenario: Looking up a word in a physical dictionary. You don't start from 'A'. You open it roughly to the middle. If you see words starting with 'M' and you're looking for 'Computer', you know you need to look in the first half. You've just eliminated half the dictionary in one go!

// Let's find 25 in this SORTED list
List: [4, 8, 12, 16, 20, 25, 30]

Step 1: Find the middle. The list has 7 items. Middle is item 4 (value 16).
        Is 16 == 25? No. 25 is bigger than 16.
        So, we can IGNORE the entire first half.
        New list to search: [20, 25, 30]

Step 2: Find the new middle. It's 25.
        Is 25 == 25? Yes! Found it.

-- ASCII Diagram --

[4, 8, 12,  16,  20, 25, 30]
             ^
             |
           Middle. Is 25 > 16? Yes.

[--, --, --, --,  20, 25, 30]  (Ignore first half)
                     ^
                     |
                   New Middle. Found!
  • Good for: Large, sorted lists. It's incredibly fast!
  • The Catch: The list MUST be sorted. If it isn't, this method won't work.
  • Efficiency (Big O Notation): We call its performance O(log n). This is super efficient! To search 1 million items, it would take a maximum of about 20 checks, compared to a million for linear search.
Image Suggestion: A vibrant, stylized illustration showing two paths. One path is a long, winding road labeled "Linear Search" with a person checking every single stone. The other path is a series of quick jumps across a canyon, labeled "Binary Search," where each jump gets the person halfway across. The style should be modern and colorful, appealing to a young adult.

Part 2: The Art of Sorting - Bringing Order to the Digital Market

Sorting is the process of arranging items in a particular order (e.g., A-Z, lowest to highest, earliest to latest). Think of KCSE results being ranked or products on Jumia being sorted by price.

A. Bubble Sort: The "Sinking Stone" Method

This is one of the simplest sorting methods to understand. It repeatedly steps through the list, compares adjacent pairs of items, and swaps them if they are in the wrong order. The smaller (or larger, depending on how you're sorting) items "bubble" to the top of the list.


// Let's sort this list: [5, 1, 4, 2] in ascending order.

Pass 1:
( [5, 1], 4, 2 ) -> Swap. List is now [1, 5, 4, 2]
( 1, [5, 4], 2 ) -> Swap. List is now [1, 4, 5, 2]
( 1, 4, [5, 2] ) -> Swap. List is now [1, 4, 2, 5]
// The largest number, 5, is now at the end.

Pass 2:
( [1, 4], 2, 5 ) -> No swap.
( 1, [4, 2], 5 ) -> Swap. List is now [1, 2, 4, 5]
// The second largest, 4, is now in place.

The list is now sorted! [1, 2, 4, 5]
  • Good for: Teaching the concept of sorting. It's very visual.
  • The Catch: It's very slow for larger lists, one of the least efficient.
  • Efficiency (Big O Notation): O(n²). The time it takes grows exponentially as the list gets bigger.

B. Selection Sort: The "Best-in-Show" Method

This method works by repeatedly finding the minimum element from the unsorted part of the list and putting it at the beginning. It's like a farmer at a market stall picking the very best mango from a pile, putting it on display, then picking the next-best one from the remaining pile, and so on.

Image Suggestion: A friendly Kenyan market vendor sorting a pile of avocados. The vendor is carefully picking the best-looking avocado (the "minimum") from a large, unsorted pile and placing it into a neat, sorted row on the stall table. The background should show a bustling, colorful open-air market.

// Let's sort this list: [29, 10, 14, 37]

Pass 1:
- Find the smallest in [29, 10, 14, 37]. It's 10.
- Swap it with the first element (29).
- List is now: [10, 29, 14, 37]
// 10 is now in its correct, final position.

Pass 2:
- Look at the unsorted part: [29, 14, 37]. Find the smallest. It's 14.
- Swap it with the first element of this part (29).
- List is now: [10, 14, 29, 37]
// 14 is now in its correct, final position.

Pass 3:
- Look at the unsorted part: [29, 37]. The smallest is 29.
- It's already in the right place, so no swap.
- List is sorted: [10, 14, 29, 37]
  • Good for: Small lists. It's conceptually simple.
  • The Catch: Also inefficient for large lists.
  • Efficiency (Big O Notation): O(n²). Similar performance to Bubble Sort, but often slightly faster in practice due to fewer swaps.

So Why Does This All Matter?

You use these concepts every single day without even realizing it!

  • Your Phone Contacts: They are sorted alphabetically so you can use a fast search to find who you want to call.
  • E-commerce like Jumia/Kilimall: When you sort products by "Price: Low to High", that's a sorting algorithm at work.
  • M-Pesa Statements: They are sorted by date, making it easy for you to find a transaction from a specific day.
  • University Admission Systems: Student records are stored and sorted by their unique admission number, allowing for a fast binary search to pull up your details.

Understanding how to organize and find data efficiently is a superpower in computer science. It's the difference between a fast, responsive app and a slow, frustrating one. As you continue your journey, you'll learn even more powerful algorithms like Merge Sort and Quick Sort, but they all build on these fundamental ideas.

Keep practicing, stay curious, and you'll be building the next generation of amazing Kenyan tech in no time! Kazi nzuri!

Pro Tip

Take your own short notes while going through the topics.

Previous Queues
KenyaEdu
Add KenyaEdu to Home Screen
For offline access and faster experience