Search Algorithms Flashcards

1
Q

Time complexity of linear search

A

O(n)

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

Time complexity of binary search

A

O(log n)

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

Time complexity of hash table search

A

Without collision resolution: O(1)
With collision resolution: O(n)

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

For a hash table, a hash function should:

A
  1. Take an input key of any length
  2. Return a location of fixed length
  3. Location should be unique to the key

Returns an output hash index of fixed size

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

For a hash table, a key should be:

A

Immutable (so that its hashed location is always the same)

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

Features of an ideal hash function

A
  • Use all input data
  • Small difference in input results in large difference in hash value
  • Output should be uniformly distributed across all possible hash values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The key steps for Binary Search are:

A
  1. Handle the base case (empty/single-element array)
  2. Initialise start = 0 and end = array.length - 1
  3. Determine middle index mid
  4. Check item at mid:
    • if match, return mid
    • if search key less than mid item, update end to mid - 1
    • if search key greater than mid item, update start to mid + 1
  5. If start > end (no match found), return none or display/raise error, else repeat from step 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

collision resolution

A

open addressing (generation of new location):
1. hash key to location
2. if key matches store key and value pair at location
3. else, rehash key to new location and repeat step 2
4. if no more location available return error
close addressing (each location in the table is a linked list):
1. hash key to location
2. check if linked list at location have the key (yes, end/ update)
3. else, add key-pair value to end of linked list

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