Theory
Sorting and searching are two of the most important operations in DSA. Understanding their time complexities helps us select an efficient algorithm for a problem. Linear Search works on unsorted data, while Binary Search requires sorted data.
Example
const numbers = [40, 10, 30, 20];
numbers.sort((a, b) => a - b);
const target = 30;
console.log(numbers.includes(target));
Expected Output
true
Practice
Compare Linear Search and Binary Search on a sorted array.
Interview Questions
- Why is sorting useful before binary search?
- What is the difference between linear and binary search?
- What is the time complexity of binary search?
- Which sorting algorithms do you know?