Unit 10: Sorting and searching Flashcards

1
Q

What is linear search?

A

A method of finding an element by checking each element sequentially until a match is found or the end is reached.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the maximum number of visits in a linear search for n elements?

A

n visits.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an advantage of linear search?

A

It does not require the array to be sorted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the return value of linearSearch() if the element is not found?

A

-1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the disadvantages of linear search?

A

Slow on large datasets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is binary search?

A

Binary search repeatedly divides a sorted array in half to find the target value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What condition must an array meet to use binary search?

A

The array must be sorted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does binary search compare to linear search for 1000 elements?

A

Linear search: ~500 visits; Binary search: ~10 visits.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the advantages of binary search?

A

Much faster than linear search on large datasets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the time complexity of binary search?

A

O(log₂ n).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the disadvantages of binary search?

A

Array must be sorted beforehand

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Big O notation?

A

To describe how the runtime of an algorithm grows relative to the input size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why is Big O notation important?

A
  • Helps predict performance on large inputs
  • Useful for comparing algorithms
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does O(1) mean?

A

Constant time — the runtime does not change with input size. = accessing array elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does O(n) mean?

A

Linear time — the runtime grows proportionally with the input size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does O(n²) mean?

A

Quadratic time — runtime grows proportionally to the square of the input size. = Nested Loops

17
Q

Why is sorting important before binary search?

A

Binary search requires a sorted array; otherwise, it won’t find the correct element.

18
Q

What is Merge Sort?

A

Merge Sort is a divide-and-conquer algorithm. It splits the array into two halves, recursively sorts them, and then merges the sorted halves back together.

19
Q

What is Selection Sort?

A

Selection Sort is a simple sorting algorithm that repeatedly selects the smallest (or largest) element from the unsorted part of the list and swaps it with the first unsorted element.

20
Q

What is space complexity in algorithms?

A

Space complexity refers to the amount of memory an algorithm needs to run, relative to the size of the input. It includes both auxiliary space (extra space used) and total space (input data plus extra space). Examples:

O(1): Constant space.

O(n): Space proportional to the input size.
20
Q

How does Selection Sort work step-by-step?

A
  1. Find the smallest element in the unsorted part of the list.
  2. Swap it with the first unsorted element.
  3. Repeat the process for the remaining unsorted part until the list is sorted.
21
Q

What is a major disadvantage of Selection Sort?

A

Selection Sort is inefficient for large lists because of its O(n²) time complexity, making it slower compared to more advanced algorithms like Quick Sort or Merge Sort.

22
Q

What does this method do?

public static int linearSearch(int[] a, int value) {
  for (int i = 0; i < a.length; i++) {
    if (a[i] == value) return i;
  }
  return -1;
}
A

It performs a linear search and returns the index of the found value or -1 if not found.