Binary Search vs Linear Search Real Cost Difference
📖 Scenario: Imagine you have a list of product prices in a store. You want to find if a certain price exists in the list. There are two ways to search: checking each price one by one (linear search) or using a faster method if the list is sorted (binary search).
🎯 Goal: You will create a list of prices, set a target price to find, write both linear and binary search functions, and then print the results showing which method found the price and how many steps each took.
📋 What You'll Learn
Create an array called
prices with these exact numbers: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]Create a variable called
targetPrice and set it to 70Write a function called
linearSearch that takes prices and targetPrice and returns an object with found (boolean) and steps (number of checks)Write a function called
binarySearch that takes prices and targetPrice and returns an object with found (boolean) and steps (number of checks)Print the results of both searches showing if the price was found and how many steps each took
💡 Why This Matters
🌍 Real World
Searching for items quickly in sorted lists like product prices, names, or IDs is common in apps and websites.
💼 Career
Understanding search algorithms helps in optimizing software performance and making user experiences faster.
Progress0 / 4 steps