1.2 Fundamental Algorithms Flashcards

1
Q

Time Complexity of Linear Search and Binary Search.

A

Linear Search: O(n)
Binary Search: O(log n)

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

State the average-case and worst-case time complexity of quicksort.

A

average-case : O(n log n)
worst-case : O(n^2)

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

State the time complexity of bubble sort.

A

O(n^2)

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

The implementation of the quicksort utilised the last data element of the array as the pivot.

Two programmmers carried out an experiment to test the execution time of two sorting algorithms using an array of sorted data containing 8 elements.

One of them used quicksort while the other used bubble sort. The one who used bubble sort discovered that his execution time was shorter as compared to the one who used quicksort.
Comment in this result.

A

Since the implementation of the quicksort utilised the last data element of the array as the pivot, this quicksort algorithm will have the worst-case time complexity of O(n^2) since the chosen pivot is the largest element.

Since bubble sort is used on a sorted array, it will have the best time complexity of O(n).

However, time efficiency only describes how execution time scales the amount of data and is not indicative of the execution time for small arrays such as 8 elements.

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

Explain what a hash key is.

A

A hash key is used to generate a hash index in a hash table to search for or store a record.

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

State the worst-case time complexity of a hash table search.

A

O(n)

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

State 3 features of a good hashing algorithm.

A
  1. uses all the input data
  2. generates very different hash values for similar strings
  3. uniformly distributes the data across the entire set of possible hash values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly