Sorting and Searching Algorithms Flashcards

1
Q

What are the 3 components of a Bubble sort?

A
  • Outer loop: Loops through the list until no swaps have been made
  • Inner loop: Loops through every item in the array
  • If-statement: Compares 2 items and swaps if first is greater than second
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the Big O efficiency of a Bubble sort?

A

O (n^2)

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

Why are Bubble sort algorithms inefficient?

A

They require multiple loops through data that has already been sorted

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

How does a Binary search work?

A
  • Choose a midpoint and divide data into two halves
  • If the key is smaller than the midpoint, delete the right half
  • if the key is larger than the midpoint, delete the left half
  • repeat until item is found or there are no data items remaining
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Big O efficiency of a Binary search?

A

O (log n)

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

How does the partition step of a Quicksort work?

A
  • A pivot value is chosen
  • If the rightmost value is larger than the pivot, the next item is compared
  • If the rightmost value is smaller than the pivot, the two numbers are swapped.
  • After a swap, the next item after the swapped item on the side closest to the pivot will be compared
  • After all swaps have been completed, the pivot value will never be swapped again as it has been sorted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does the recursive step of a Quicksort work?

A
  • Repeat the partition step on all items to the left of the pivot
  • Repeat the partition step on all items to the right of the pivot
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What swaps would occur if a quicksort was used on this set of numbers? Take 15 as the pivot value

15, 32, 4, 7, 89, 12, 69

A
  • 12, 32, 4, 7, 89, 15, 69
  • 12, 15, 4, 7, 89, 32, 69
  • 12, 7, 4, 15, 32, 69
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Big O efficiency of a Quicksort

A

0 (n log n)

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

What is the Big O efficiency of a linear search?

A

O (n)

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

How does adding a sentinel value to a linear search increase the efficiency?

A

Guarantees the search item is found, so eliminates the need to check if the end of the list has been reached.

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