Searching, Sorting, Selection and Hashing Short Notes for GATE CS
Searching, Sorting, Selection and Hashing short notes for GATE CS: 5 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice q
searching sorting selection and hashing short notes
Sorting Operation Cheat Sheet
Quick Reference: Sorting Operation Counts
Algorithm
Best Case Comparisons
Worst Case Comparisons
Worst Case Data Movements
Bubble Sort
Θ(n)
Θ(n2)
Θ(n2) (Swaps = Inversions)
Insertion Sort
Θ(n)
Θ(n2)
Θ(n2) (Shifts = Inversions)
Selection Sort
Θ(n2)
Θ(n2)
Θ(n) (Exactly n−1 swaps)
Merge Sort
Θ(nlogn)
Θ(nlogn)
Θ(nlogn) (n−1 total merges)
Quick Sort
Θ(nlogn)
Θ(n2)
Θ(n2)
Core Rules for Exam Analysis:
Inversions dictate adaptive sorts: For Bubble and Insertion sort, count inversions to find exact swaps/shifts.
Selection sort is rigid: Comparisons are always 2n(n−1).
Void Merge condition:max(L)≤min(R).
Loop bound math:0 to K inclusive = K+1 iterations.
Binary and Bitonic Search Cheat Sheet
Quick Reference: Binary and Bitonic Search
Operation
Time Complexity
Key Condition / Formula
Standard Binary Search
O(logn)
mid = low + (high - low) / 2
Find Bitonic Peak
O(logn)
If A[\text{mid}] < A[\text{mid}+1], low = mid + 1
Bitonic Array Search
O(logn)
1. Find peak. 2. Ascending BS on left. 3. Descending BS on right.
Exam Readiness Rules:
Distinct Elements: The O(logn) peak finding relies on the array having distinct elements (or strict inequalities). If duplicates are allowed, worst-case can degrade to O(n).
Boundary Checks: Always ensure mid + 1 does not go out of bounds when checking A[\text{mid}] < A[\text{mid}+1].
Recursive Arithmetic: For recursive binary search, the number of arithmetic operations (additions, divisions) is proportional to the depth of the recursion tree, which is O(logn).
Comparison Bounds Cheat Sheet
Quick Reference: Comparison Bounds
Problem
Optimal Comparisons
Method / Condition
Find Min (or Max)
n−1
Linear scan.
Find Min AND Max
⌈23n⌉−2
Pairwise comparison.
Find 2nd Largest
n+⌈log2n⌉−2
Tournament tree.
Find k-th Smallest
O(n) expected
Quickselect (randomized pivot).
Find k-th Smallest
O(n) worst-case
Median of Medians (deterministic).
Exam Readiness Rules
Read the prompt carefully: "Not the largest" = "The smallest".
Tournament tree logic: The runner-up must have lost directly to the champion.
Median of Medians: Group size of 5 is the smallest odd number that guarantees the O(n) recurrence (1/5+7/10<1). Groups of 3 fail to guarantee linear time.
2 more cards in this chapter
Try a question
Answer it here to see how it works. Nothing is recorded until you sign in.
Question 1
Level 1: Warm-up
Which of the following classic sorting algorithms performs exactly the same number of comparisons regardless of whether the input array is already sorted, reverse sorted, or randomly ordered?
Question 2
Level 1: Warm-up
For an array of n distinct elements arranged in strictly descending (reverse sorted) order, what is the total number of inversions present in the array?
Question 3
Level 1: Warm-up
In the context of sorting algorithms, an "inversion" in an array A is defined as a pair of indices (i,j) such that:
Question 4
Level 1: Warm-up
What is the exact number of comparisons performed by the Insertion Sort algorithm in its best-case scenario (when the input array is already sorted in ascending order) for an array of size n?
Question 5
Level 1: Warm-up
When sorting an array of n elements using the standard top-down Merge Sort algorithm, what is the total number of merge operations performed?
Question 6
Level 1: Warm-up
In Merge Sort, a merge operation between a sorted left subarray L and a sorted right subarray R is defined as "void" if max(L)≤min(R). During such a void merge operation, how many elements from R are placed before the first element of L in the merged output?
Question 7
Level 1: Warm-up
The "operation count fingerprint" of a sorting algorithm primarily helps a student to:
Question 8
Level 1: Warm-up
In the standard Bubble Sort algorithm, the total number of swap operations performed on an arbitrary array is exactly equal to which of the following?
Question 9
Level 1: Warm-up
For an array of n elements, how many comparisons does the standard Selection Sort algorithm perform, regardless of the initial order of the elements?
Question 10
Level 1: Warm-up
During a single partition step in the Quick Sort algorithm on a subarray of size k, how many comparisons are made between the array elements and the pivot?
Free preview ends here
Login to view the complete short notes
Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.
Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.
Built around you, not around a syllabus PDF
Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.
Revision that hits your weak spots
We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.
Questions calibrated to the real exam
Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.
Notes written for recall, not for volume
Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.
One place for everything
Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.
Honest progress
No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.
Unlock the whole course
Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.
Searching, Sorting, Selection and Hashing Short Notes for GATE CS
Searching, Sorting, Selection and Hashing short notes for GATE CS: 5 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.
Sorting Operation Cheat Sheet
Quick Reference: Sorting Operation Counts
Algorithm
Best Case Comparisons
Worst Case Comparisons
Worst Case Data Movements
Bubble Sort
Θ(n)
Θ(n2)
Θ(n2) (Swaps = Inversions)
Insertion Sort
Θ(n)
Θ(n2)
Θ(n2) (Shifts = Inversions)
Selection Sort
Θ(n2)
Θ(n2)
Θ(n) (Exactly n−1 swaps)
Merge Sort
Θ(nlogn)
Θ(nlogn)
Θ(nlogn) (n−1 total merges)
Quick Sort
Θ(nlogn)
Θ(n2)
Θ(n2)
Core Rules for Exam Analysis:
Inversions dictate adaptive sorts: For Bubble and Insertion sort, count inversions to find exact swaps/shifts.
Selection sort is rigid: Comparisons are always 2n(n−1).
Void Merge condition:max(L)≤min(R).
Loop bound math:0 to K inclusive = K+1 iterations.
Binary and Bitonic Search Cheat Sheet
Quick Reference: Binary and Bitonic Search
Operation
Time Complexity
Key Condition / Formula
Standard Binary Search
O(logn)
mid = low + (high - low) / 2
Find Bitonic Peak
O(logn)
If A[\text{mid}] < A[\text{mid}+1], low = mid + 1
Bitonic Array Search
O(logn)
1. Find peak. 2. Ascending BS on left. 3. Descending BS on right.
Exam Readiness Rules:
Distinct Elements: The O(logn) peak finding relies on the array having distinct elements (or strict inequalities). If duplicates are allowed, worst-case can degrade to O(n).
Boundary Checks: Always ensure mid + 1 does not go out of bounds when checking A[\text{mid}] < A[\text{mid}+1].
Recursive Arithmetic: For recursive binary search, the number of arithmetic operations (additions, divisions) is proportional to the depth of the recursion tree, which is O(logn).
Comparison Bounds Cheat Sheet
Quick Reference: Comparison Bounds
Problem
Optimal Comparisons
Method / Condition
Find Min (or Max)
n−1
Linear scan.
Find Min AND Max
⌈23n⌉−2
Pairwise comparison.
Find 2nd Largest
n+⌈log2n⌉−2
Tournament tree.
Find k-th Smallest
O(n) expected
Quickselect (randomized pivot).
Find k-th Smallest
O(n) worst-case
Median of Medians (deterministic).
Exam Readiness Rules
Read the prompt carefully: "Not the largest" = "The smallest".
Tournament tree logic: The runner-up must have lost directly to the champion.
Median of Medians: Group size of 5 is the smallest odd number that guarantees the O(n) recurrence (1/5+7/10<1). Groups of 3 fail to guarantee linear time.
Global transitive properties ⟹ Local adjacent checks ⟹Single Pass O(N).
Searching, Sorting, Selection and Hashing: Solved Questions with Step-by-Step Explanations (10 Problems)
Question 1 · AlgorithmsMCQ
Which of the following classic sorting algorithms performs exactly the same number of comparisons regardless of whether the input array is already sorted, reverse sorted, or randomly ordered?
A.
Insertion Sort
B.
Bubble Sort (with early termination flag)
C.
Selection Sort
D.
Quick Sort
Correct Answer:
C
Step-by-Step Solution
Key idea: This is a comparative operation count question, testing knowledge of which algorithms are "adaptive" versus "non-adaptive" in their comparison behavior.
Step 1: Insertion Sort is adaptive; it makes n−1 comparisons on a sorted array but 2n(n−1) on a reverse sorted array.
Step 2: Bubble Sort with an early termination flag is adaptive; it makes n−1 comparisons on a sorted array.
Step 3: Quick Sort's comparison count heavily depends on pivot selection and the initial order of the array.
Step 4: Selection Sort always scans the entire unsorted portion to find the minimum element, performing exactly 2n(n−1) comparisons in all cases.
Answer: C
Question 2 · AlgorithmsMCQ
For an array of n distinct elements arranged in strictly descending (reverse sorted) order, what is the total number of inversions present in the array?
A.
n
B.
n−1
C.
racn(n−1)2
D.
racn(n+1)2
Correct Answer:
C
Step-by-Step Solution
Key idea: This is a direct application of the inversion definition to a specific worst-case input configuration.
Step 1: In a strictly descending array, every element is greater than every element that appears after it.
Step 2: Therefore, every possible pair of indices (i,j) with i<j forms an inversion.
Step 3: The total number of such pairs in an array of size n is given by the combination formula (2n)=2n(n−1).
Answer: C
Question 3 · AlgorithmsMCQ
In the context of sorting algorithms, an "inversion" in an array A is defined as a pair of indices (i,j) such that:
A.
i<j and A[i]>A[j]
B.
i>j and A[i]<A[j]
C.
i<j and A[i]<A[j]
D.
i>j and A[i]>A[j]
Correct Answer:
A
Step-by-Step Solution
Key idea: This is a definition recall question, recognisable because it asks for the precise mathematical condition of an inversion.
Step 1: Recall that an inversion represents a pair of elements that are out of their natural sorted order.
Step 2: For elements to be out of order, the element appearing earlier in the array (smaller index i) must be strictly greater than the element appearing later (larger index j).
Step 3: Therefore, the conditions are i<j (index order) and A[i]>A[j] (value order).
Answer: A
Question 4 · AlgorithmsMCQ
What is the exact number of comparisons performed by the Insertion Sort algorithm in its best-case scenario (when the input array is already sorted in ascending order) for an array of size n?
A.
0
B.
1
C.
n−1
D.
2n(n−1)
Correct Answer:
C
Step-by-Step Solution
Key idea: This is a direct recall question about Insertion Sort best-case behavior, recognisable because it specifies the "best-case scenario" and "already sorted" input.
Step 1: Recall how Insertion Sort works. It iterates through the array, taking one element at a time and inserting it into its correct position in the already sorted prefix.
Step 2: In the best case, the array is already sorted.
Step 3: For each element from index 1 to n−1, the algorithm compares it with the element immediately before it.
Step 4: Since the array is sorted, the condition A[j]>A[j+1] (or equivalent) fails immediately on the first comparison.
Step 5: This results in exactly 1 comparison per element for the n−1 elements being processed, totaling n−1 comparisons.
Answer: C
Question 5 · AlgorithmsMCQ
When sorting an array of n elements using the standard top-down Merge Sort algorithm, what is the total number of merge operations performed?
A.
log2n
B.
n−1
C.
n
D.
2n(n−1)
Correct Answer:
B
Step-by-Step Solution
Key idea: This is a direct recall question about Merge Sort operation counts, recognisable because it asks for the total number of merge operations (not comparisons) for the entire sort.
Step 1: Recall the structure of top-down Merge Sort. It recursively divides the array into halves until subarrays of size 1 are reached.
Step 2: Each merge operation takes two sorted subarrays and combines them into one larger sorted subarray.
Step 3: Every merge operation reduces the total number of disjoint sorted subarrays by exactly 1.
Step 4: We start with n subarrays of size 1. We want to end up with 1 sorted subarray of size n.
Step 5: To go from n subarrays to 1 subarray, we must perform exactly n−1 merge operations, regardless of the tree shape.
Answer: B
Question 6 · AlgorithmsMCQ
In Merge Sort, a merge operation between a sorted left subarray L and a sorted right subarray R is defined as "void" if max(L)≤min(R). During such a void merge operation, how many elements from R are placed before the first element of L in the merged output?
A.
0
B.
1
C.
∣L∣
D.
∣R∣
Correct Answer:
A
Step-by-Step Solution
Key idea: This is a definition and behavior recall question for a specific Merge Sort edge case.
Step 1: The condition max(L)≤min(R) means every element in the left subarray is less than or equal to every element in the right subarray.
Step 2: During the merge process, the algorithm will compare the front of L and R. Since all elements of L are smaller, it will exhaust all elements of L first.
Step 3: Only after L is completely copied will the algorithm begin copying elements from R. Thus, zero elements of R are placed before any element of L.
Answer: A
Question 7 · AlgorithmsMCQ
The "operation count fingerprint" of a sorting algorithm primarily helps a student to:
A.
Determine the exact programming language used to implement it.
B.
Predict its exact behavior and performance on a specific input without running the code.
C.
Identify the memory address of the array elements.
D.
Convert the algorithm into a recursive form.
Correct Answer:
B
Step-by-Step Solution
Key idea: This is a conceptual definition question about the purpose of algorithmic analysis via operation counting.
Step 1: An "operation count fingerprint" refers to the precise number of comparisons and data movements an algorithm makes.
Step 2: This count is deterministic for a given algorithm and a specific input configuration (e.g., sorted, reverse sorted, or a specific number of inversions).
Step 3: By knowing this fingerprint, one can mathematically predict the algorithm's runtime behavior and efficiency on that input without needing to execute the code.
Answer: B
Question 8 · AlgorithmsMCQ
In the standard Bubble Sort algorithm, the total number of swap operations performed on an arbitrary array is exactly equal to which of the following?
A.
The number of elements in the array
B.
The number of inversions in the array
C.
The total number of comparisons made
D.
The number of unique elements in the array
Correct Answer:
B
Step-by-Step Solution
Key idea: This is a direct recall question about Bubble Sort operation counts, recognisable because it asks for the exact mathematical equivalent of the number of swaps.
Step 1: Recall how Bubble Sort works. It repeatedly steps through the list and swaps adjacent elements if they are in the wrong order.
Step 2: Each swap corrects exactly one inversion (a pair of elements that are out of order).
Step 3: The algorithm terminates when no inversions remain. Therefore, the total number of swaps is exactly equal to the initial number of inversions in the array.
Answer: B
Question 9 · AlgorithmsMCQ
For an array of n elements, how many comparisons does the standard Selection Sort algorithm perform, regardless of the initial order of the elements?
A.
n−1
B.
2n(n−1)
C.
nlog2n
D.
n2
Correct Answer:
B
Step-by-Step Solution
Key idea: This is a direct recall question about Selection Sort operation counts, recognisable because it asks for the fixed number of comparisons independent of input order.
Step 1: Recall how Selection Sort works. It finds the minimum element in the unsorted portion and swaps it into place.
Step 2: In the first pass, it makes n−1 comparisons to find the minimum of n elements.
Step 3: In the second pass, it makes n−2 comparisons, and so on, down to 1 comparison in the final pass.
Step 4: The total number of comparisons is the sum of the first n−1 integers: (n−1)+(n−2)+⋯+1=2n(n−1).
Step 5: This count is fixed and does not change whether the array is sorted, reverse sorted, or random.
Answer: B
Question 10 · AlgorithmsMCQ
During a single partition step in the Quick Sort algorithm on a subarray of size k, how many comparisons are made between the array elements and the pivot?
A.
k
B.
k−1
C.
2k(k−1)
D.
klog2k
Correct Answer:
B
Step-by-Step Solution
Key idea: This is a direct recall question about Quick Sort partition mechanics, recognisable because it isolates a single partition step and asks for the comparison count.
Step 1: Recall the standard partition process (e.g., Lomuto or Hoare). One element is chosen as the pivot.
Step 2: The algorithm iterates through the remaining elements of the subarray to compare each one against the pivot to determine which side of the partition it belongs to.
Step 3: Since the pivot itself is not compared against itself, there are exactly k−1 other elements in the subarray of size k.
Step 4: Therefore, exactly k−1 comparisons are made during that single partition step.