0
0
DSA Typescriptprogramming~30 mins

Binary Search vs Linear Search Real Cost Difference in DSA Typescript - Build Both Approaches

Choose your learning style9 modes available
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 70
Write 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
1
Create the price list
Create an array called prices with these exact numbers: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
DSA Typescript
Hint

Use const prices = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];

2
Set the target price
Create a variable called targetPrice and set it to 70
DSA Typescript
Hint

Use const targetPrice = 70;

3
Write linear search function
Write a function called linearSearch that takes prices and targetPrice and returns an object with found (boolean) and steps (number of checks). Use a for loop with variable i to check each price.
DSA Typescript
Hint

Use a for loop to check each price. Increase steps each time you check.

4
Write binary search function and print results
Write a function called binarySearch that takes prices and targetPrice and returns an object with found (boolean) and steps (number of checks). Use variables left, right, and mid to search. Then print the results of both linearSearch and binarySearch showing if the price was found and how many steps each took.
DSA Typescript
Hint

Use a while loop with left, right, and mid to find the target. Count steps each check. Then print results using console.log.