Searching and Sorting Flashcards

(20 cards)

1
Q

What is a search algorithm?

A

A search algorithm is a method used to find the position of a specific value within a data structure such as an array or list.

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

What is binary search?

A

Binary search is a search algorithm that repeatedly divides a sorted list in half to locate a target value.

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

What condition must be true before binary search can be used?

A

The data must be sorted.

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

What are the steps of binary search?

A

Find the middle element; compare with the target; if equal stop; if smaller search left half; if larger search right half; repeat until found or list empty.

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

Why is binary search efficient?

A

Each comparison halves the search space, reducing the number of comparisons needed.

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

What is the best case time complexity of binary search?

A

O(1) when the item is found immediately.

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

What is the average time complexity of binary search?

A

O(log n) because the search space is halved each step.

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

What is the worst case time complexity of binary search?

A

O(log n) when the element is at the deepest level or not found.

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

What is the main disadvantage of binary search?

A

The data must be sorted before searching.

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

What is a hash table?

A

A data structure that stores data using a hash function to determine the index where the data is stored.

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

What is a hash function?

A

A function that converts a key into an index in a hash table, for example index = key mod table_size.

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

What is a key in a hash table?

A

The value used by the hash function to determine where data should be stored.

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

What is a hash collision?

A

When two different keys produce the same index in a hash table.

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

Why do collisions occur?

A

Because the number of possible keys is larger than the number of positions in the hash table.

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

What is chaining?

A

A collision resolution method where multiple values are stored at the same index using a list or linked list.

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

What is linear probing?

A

A collision resolution method where the algorithm searches the next available space in the table sequentially.

17
Q

What is double hashing?

A

A collision resolution method where a second hash function is used to calculate a new index.

18
Q

What is the average time complexity of searching in a hash table?

A

O(1) because the hash function directly calculates the storage index.

19
Q

What is the worst case time complexity of a hash table search?

A

O(n) when many collisions occur.

20
Q

Compare binary search and hash table searching.

A

Binary search requires sorted data and has O(log n) time complexity; hash tables do not require sorted data and have average O(1) time complexity.