CHAPTER 24
Beginner
Searching and Sorting Algorithms
Updated: May 17, 2026
5 min read
# CHAPTER 24
Searching and Sorting Algorithms
1. Introduction
Data is useless if you can't find what you're looking for. Searching is the process of finding an element in a dataset. Sorting is arranging data in a specific order (ascending or descending) to make searching faster. Understanding these algorithms is a core requirement for any software engineering interview.2. Learning Objectives
By the end of this chapter, you will be able to:- Implement Linear Search and Binary Search.
- Understand Time Complexity (Big-O Notation) basics.
- Implement Bubble Sort, Selection Sort, and Insertion Sort.
- Decide which algorithm to use based on the dataset.
3. Linear Search
The simplest method. It checks every element one by one from the beginning to the end.-
Time Complexity: O(n) (Worst case: element is at the end, requires
nchecks).
- Requirement: Works on unsorted arrays.
c
4. Binary Search
Much faster, but the array MUST be sorted first. It compares the target to the middle element. If it matches, return. If it's smaller, search the left half. If larger, search the right half.- Time Complexity: O(log n) (Extremely fast. Searching 1,000,000 items takes at most 20 checks).
c
5. Bubble Sort
Repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The largest elements "bubble" to the top.- Time Complexity: O(n²) (Very slow for large datasets).
c
6. Selection Sort
Divides the array into a sorted and an unsorted part. Finds the smallest element in the unsorted part and swaps it with the first unsorted element.- Time Complexity: O(n²).
c
7. Insertion Sort
Builds the final sorted array one item at a time, much like sorting a hand of playing cards.- Time Complexity: O(n²). Very fast if the array is *almost* sorted (O(n)).
c
8. Common Mistakes
-
Using Binary Search on Unsorted Data: Binary Search logic fundamentally relies on order. If the data is unsorted, it will return
-1incorrectly.
-
Bubble Sort
jloop limit: The inner loop should go tosize - i - 1. If it goes tosize - 1, it will checkarr[j+1]which might be out of bounds!
-
Swapping logic: A proper swap requires a temporary variable (
temp). Doinga = b; b = a;makes them both identical.
9. Exercises
-
1.
Modify Bubble Sort to include a boolean
swappedflag. If a full pass happens without any swaps,breakthe loop early (Optimized Bubble Sort).
-
2.
Trace Insertion Sort on paper for the array:
{12, 11, 13, 5, 6}.
10. MCQ Quiz with Answers
Question 1
Which search algorithm requires the array to be sorted?
Question 2
What is the worst-case time complexity of Linear Search?
Question 3
Which sorting algorithm simulates sorting playing cards in your hand?
Question 4
What is the worst-case time complexity of Bubble Sort?
Question 5
Binary Search time complexity is:
Question 6
Selection Sort finds the ________ element in the unsorted portion and swaps it.
Question 7
If an array has 100 elements, in the worst case, how many comparisons does Linear Search make?
Question 8
For the same 100 elements, roughly how many comparisons does Binary Search make?
Question 9
Bubble sort pushes the _______ element to the end of the array in each outer loop pass.
Question 10
Why is a temporary variable needed when swapping two array elements?
11. Interview Questions
- Q: Explain why Binary Search is O(log n) time complexity.
- Q: When would Insertion Sort be a better choice than Bubble Sort or Selection Sort?
-
Q: Explain the swapping bug
a = b; b = a;and how to fix it using a temporary variable or XOR bitwise logic.