Searching and Sorting Algorithms Flashcards

(12 cards)

1
Q

What is a linear search algorithm?

A

An algorithm that looks through each item in a set until the search key is found or every item has been inspected.

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

What are the advantages and disadvantages of linear search?

A

+ Only option for unsorted lists
- Inefficient: O(n)

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

What does the efficiency of a linear search depend on?

A
  • Size of the dataset
  • The location of the item to search for
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a binary search algorithm?

A

An algorithm that looks at the middle item, if it is less look at the middle item in the lower half, if it is more look at the middle item in the upper half. This is repeated until it is found or low pointer is above the high pointer

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

What are the advantages and disadvantages of binary search?

A

+ Very efficient for large databases - O(log n)
- Can be more difficult to implement
- Must be a sorted list

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

How do you search in a binary search tree?

A

Using an in-order traversal

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

How does bubble sort work?

A

Each item in a list is compared to the one after it. If the item after is smaller that the current one then they swap until no swaps are made and it is sorted.

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

What are the disadvantages of bubble sort?

A
  • Inefficient time wise as time complexity of O(n2)
  • Algorithm continues to iterate over the list even if no further swaps are required
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can bubble sort be improved?

A
  • Reducing the number of comparisons after each pass
  • A sorted flag that can stop the sort if no swaps are made
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does merge sort work?

A
  • Example of a divide and conquer algorithm where the program is broken down into smaller units
  • List is continuously divided until there is only one item in each
  • Sublists are combined in order until the list is fully sorted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the disadvantages of merge sort?

A

If done recursively is not very memory efficient

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

What are the advantages of merge sort?

A

Time efficient with an efficiency of O(nlogn)

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