Searching and Sorting Algorithms

Welcome to the world of algorithms! In this chapter, we are going to look at the "recipes" computers use to find information and put it in order. Whether you are searching for a contact on your phone or sorting your Spotify playlist by artist, these algorithms are working behind the scenes.
Don't worry if this seems tricky at first; we will break every process down into simple, bite-sized steps!

Prerequisite Check: Before we start, remember that a list (or array) is just a collection of data items stored together, like a row of lockers in a school hallway.


Section 1: Searching Algorithms

Searching is simply finding a specific item (the target) within a list of data.

1. Linear Search

The Linear Search is the simplest way to find something. It starts at the very beginning of the list and looks at every single item, one by one, until it finds what it’s looking for or reaches the end.

Analogy: Imagine you are looking for a specific green sock in a messy drawer. You pick up the first sock, check it, then the second, then the third, until you find the green one.

How it works (Step-by-Step):

1. Start at the first item in the list.
2. Compare this item with the target value you are looking for.
3. If it matches, success! You've found it.
4. If it doesn't match, move to the next item in the list.
5. Repeat steps 2-4 until you find the item or reach the end of the list.

Quick Review Box:
- Pre-requisites: None! The list can be in any order (unsorted).
- Best for: Small lists.
- Downside: It is very slow for huge lists because you might have to check every single item.

2. Binary Search

The Binary Search is much faster and more "intelligent" than a linear search, but it has one very important rule: the list MUST be sorted (e.g., in alphabetical or numerical order) before you start.

Analogy: Think of a physical dictionary. You don't start at page 1 to find the word "Zebra." You jump to the middle, see that "M" is there, realize "Z" is later, and ignore the first half of the book entirely!

How it works (Step-by-Step):

1. Find the middle item in the sorted list.
2. Compare the middle item to your target value.
3. If the middle item is the target, you are done!
4. If the target is smaller than the middle item, throw away the right half of the list.
5. If the target is larger than the middle item, throw away the left half of the list.
6. Repeat the process with the remaining half until the item is found.

To find the middle position, computers often use this formula:
\( middle = (first + last) / 2 \)

Common Mistake: Many students forget that Binary Search only works on sorted lists. If the list is a mess, Binary Search will fail!

Did you know? If you had a list of 1,000,000 items, a Linear Search might take 1,000,000 steps, but a Binary Search would take only about 20 steps! That is the power of "divide and conquer."

Key Takeaway: Use Linear for messy, small lists; use Binary for ordered, large lists.


Section 2: Sorting Algorithms

Sorting means putting data into a specific order (usually ascending, like 1 to 10 or A to Z).

1. Bubble Sort

Bubble Sort is a simple algorithm that steps through the list, compares adjacent pairs, and swaps them if they are in the wrong order. The largest values "bubble" up to the end of the list.

How it works (Step-by-Step):

1. Look at the first two items in the list.
2. If the first is larger than the second, swap them.
3. Move to the next pair (2nd and 3rd items) and repeat the comparison/swap.
4. Continue until you reach the end of the list (this is called one pass).
5. Repeat the whole process until you complete a pass with no swaps at all.

Quick Review Box:
- Pros: Very simple to write code for.
- Cons: Very slow and inefficient for large lists.

2. Insertion Sort

The Insertion Sort builds a sorted list one item at a time. It takes each item from the "unsorted" part and inserts it into the correct position in the "sorted" part.

Analogy: Imagine you are being dealt cards in a game. You pick up one card at a time and slide it into the correct place in your hand so that your cards are always in order.

How it works (Step-by-Step):

1. Start with the second item in the list (assume the first item is a "sorted list" of one).
2. Compare it to the items to its left.
3. Insert it into the correct position so the items to its left are in order.
4. Move to the next item and repeat until all items are processed.

Memory Aid: Think of "Inserting" a book into a shelf or a card into a hand.

3. Merge Sort

The Merge Sort is a Divide and Conquer algorithm. It is much more complex but also much faster for large amounts of data.

How it works (Step-by-Step):

1. Divide: Split the list in half repeatedly until every item is in its own tiny list of one.
2. Conquer: Merge the tiny lists back together in pairs. As you merge them, put the items in the correct order.
3. Repeat the merging until you have one single, fully sorted list.

Analogy: If you had to sort 100 papers, you might give 50 to a friend and 50 to another. They sort their piles, and then you "merge" them back together by looking at the top of each pile.

Key Takeaway:
- Bubble Sort: Slow, swaps neighbors.
- Insertion Sort: Better for small lists, slides items into place.
- Merge Sort: Very fast for big lists, uses "Divide and Conquer."


Section 3: Summary Table

Use this table to quickly compare the algorithms for your exam revision!

Algorithm: Linear Search
Requires Sorted List? No
Efficiency: Low

Algorithm: Binary Search
Requires Sorted List? YES
Efficiency: High

Algorithm: Bubble Sort
Description: Swaps neighbors until sorted.
Efficiency: Low

Algorithm: Insertion Sort
Description: Places items into the correct position one by one.
Efficiency: Medium

Algorithm: Merge Sort
Description: Splits list apart and merges it back in order.
Efficiency: Very High

Final Tip: In the exam, you might be shown a few lines of code or pseudocode and asked "Which algorithm is this?"
- Look for swapping neighbors (Bubble).
- Look for finding a midpoint (Binary).
- Look for splitting lists in half (Merge).

You've got this! Keep practicing by tracing these algorithms with a small list of numbers on paper.