How is Binary Search applied in real-world scenarios?
Searching for a word in a dictionary, finding a contact in a sorted phone book, searching for a value in a sorted database index.
Flip to see [answer/question]
Flip to see [answer/question]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
How is Binary Search applied in real-world scenarios?
Searching for a word in a dictionary, finding a contact in a sorted phone book, searching for a value in a sorted database index.
How is Merge Sort applied in real-world scenarios?
External sorting of large datasets that don't fit in memory, sorting large files, used in various sorting algorithms in databases.
How is Linear Search applied in real-world scenarios?
Searching for an item in a small, unsorted list, finding a specific file in a directory.
How is Insertion Sort applied in real-world scenarios?
Sorting a hand of playing cards, sorting small datasets, used as a subroutine in more complex sorting algorithms.
How is Selection Sort applied in real-world scenarios?
Although not very efficient, it can be used in scenarios where memory usage is a primary concern due to its in-place sorting nature.
What are real-world applications of recursion?
File system traversal, parsing complex data structures (like XML or JSON), implementing tree-based algorithms, and solving problems that can be broken down into smaller, self-similar subproblems.
How are sorting algorithms used in databases?
Sorting is essential for indexing data, optimizing query performance, and presenting data in a structured manner.
How are searching algorithms used in databases?
Searching algorithms are used to efficiently locate specific records within a database, enabling quick retrieval of information.
How are sorting algorithms used in search engines?
Search engines use sorting algorithms to rank search results based on relevance, ensuring that the most relevant results appear at the top of the page.
How are searching algorithms used in search engines?
Search engines use searching algorithms to quickly locate web pages that match a user's search query.
java
public static int recursiveBinarySearch(ArrayList<Integer> array, int n, int left, int right) {
int middle = (left + right) / 2;
if (left > right) { return -1; }
else if (n == array.get(middle)) { return middle; }
else if (n < array.get(middle)) { recursiveBinarySearch(array, n, left, middle - 1); }
else if (n > array.get(middle)) { recursiveBinarySearch(array, n, middle + 1, right); }
}
The recursive calls don't return a value. Should be return recursiveBinarySearch(...).
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9));
int index = recursiveLinearSearch(arr, 8, 0);
System.out.println(index);
2
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 5, 8, 9));
int index = recursiveBinarySearch(arr, 5, 0, arr.size() - 1);
System.out.println(index);
2
Identify the error in the following code:
java
public static int recursiveLinearSearch(ArrayList<Integer> array, int n, int startingIndex) {
if (array.get(startingIndex) == n) { return startingIndex; }
else if (startingIndex == array.size() - 1) { return -1; }
else { recursiveLinearSearch(array, n, startingIndex); }
}
The recursive call does not increment startingIndex, leading to infinite recursion. Should be recursiveLinearSearch(array, n, startingIndex + 1).
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9));
int index = recursiveLinearSearch(arr, 10, 0);
System.out.println(index);
-1
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 5, 8, 9));
int index = recursiveBinarySearch(arr, 10, 0, arr.size() - 1);
System.out.println(index);
-1
Why must a list be sorted for Binary Search?
Binary search works by repeatedly dividing the search interval in half. This requires knowing whether the target value is in the lower or upper half, which is only possible if the list is sorted.
What is the core idea behind Merge Sort?
Divide the list into smaller sublists, recursively sort the sublists, and then merge them back together in sorted order.
What is the time complexity of Linear Search?
O(n) in the worst case, where n is the number of elements in the list.
What is the time complexity of Binary Search?
O(log n), where n is the number of elements in the list.
What is the time complexity of Insertion Sort?
O(n^2) in the worst and average cases, where n is the number of elements in the list.
What is the time complexity of Selection Sort?
O(n^2) in all cases, where n is the number of elements in the list.
What is the time complexity of Merge Sort?
O(n log n) in all cases, where n is the number of elements in the list.
What is the space complexity tradeoff of Merge Sort?
Merge Sort requires more memory due to the creation of temporary arrays during the merge process.
What is the benefit of using recursion?
Recursion can make code more concise and easier to read for certain problems.
What is a potential drawback of using recursion?
Recursion can lead to stack overflow errors if the base case is not reached or the recursion depth is too large.