HackIndia Logo
HackathonsNewsSkillsGrowth
HackIndia LogoContact: +91 9910125804

General

  • Companies
  • Colleges
  • Board of Advisors
  • Brand Ambassadors
  • FAQ
  • Contact
  • Report a Bug

Hackathons

  • Upcoming Hackathons
  • Judges
  • Mentors
  • Partners
  • Participating Colleges

Resources

  • About Us
  • Branding
  • Latest News
  • Growth
  • Skills
  • Ecosystem
  • Forum
  • Ideas
  • Prompt Optimizer

© 2026 HackIndia. All rights reserved.

Privacy PolicyTerms of ServiceCode of ConductSitemap
    HackIndia
    HackathonsNewsInternshipsSkillsGrowth
    ← AlgorithmsLesson 2 of 8 4 min

    Algorithms

    Sorting and searching basics

    • +10 XP for this lesson
    • +100 XP when the skill is proved
    • Nobody here has proved it yet
    In 30 seconds
    • Use sorting to make a problem easier before coding.
    • Pick binary search only when the answer space is ordered.
    • Add one sorted problem and one searched problem to your repo.
    Key point

    Core idea

    Sorting changes the shape of a problem. Searching uses that shape to avoid checking every item.

    Before coding, ask: can sorted order remove choices, reveal duplicates, or make yes/no decisions monotonic?

    When sorting helps

    In placement rounds, sorting is often the first move when order does not matter in the final answer. It helps with duplicates, intervals, pairing, closest values, and ranking.

    Sorting costs time, usually O(n log n). That is fine if it makes the rest of the solution simple and correct.

    ← Lesson 1Skip to lesson 3 →
    Working towards
    Algorithms +100 XP

    0 of 8 lessons done · proved by a submitted project with a public repository

    See the track
    Your to-do

    Sort-first checklist

    Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.

    0 of 5 done

    Common mistake

    Common mistake

    Do not sort just because it feels neat. If the problem asks for original indices, sorting can lose information.

    Fix this by sorting pairs like (value, index), or by using a map when original positions matter.

    When search helps

    Use linear search for small unsorted data or when no order exists. Use binary search when the array is sorted, or when you can test a yes/no condition over an ordered answer space.

    The yes/no condition must flip only once. Example: if speed k can finish work on time, then any bigger speed also works. That is binary-searchable.

    Example

    Example: lower bound

    Lower bound finds the first index where arr[index] is greater than or equal to target. It is useful for insert position, counts, and range queries.

    Notice the loop keeps the answer inside left. No special case is needed after the loop.

    def lower_bound(arr, target):
        left, right = 0, len(arr)
    
        while left < right:
            mid = (left + right) // 2
    
            if arr[mid] < target:
                left = mid + 1
            else:
                right = mid
    
        return left
    
    nums = [2, 4, 4, 7, 9]
    print(lower_bound(nums, 4))   # 1
    print(lower_bound(nums, 5))   # 3
    print(lower_bound(nums, 10))  # 5
    Tip

    Use AI carefully

    You can ask ChatGPT or Gemini for edge cases after you write your approach. Paste your problem statement, your idea, and your code.

    Check what it returns. Test empty input, one item, duplicates, negative numbers, already sorted input, and target not found.

    Your to-do

    Do this now

    Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.

    0 of 5 done

    Quick check

    0 of 2 right
    1.A problem asks you to find duplicates, and original order does not matter. What is a good first move?
    2.When should you use binary search?

    Answer the quick check to finish this lesson.