Search and sort algorithms info Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

how does a linear search work?

A

runs through each term in the array and compares it to the search item.
loops until the item is found or until the end of the list is reached.

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

what is the time complexity of a linear search algorithm?

A
  • worst case - item is not in list O(n)
  • average case - O(n)
  • best case - item is the first item in the list O(1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the space complexity of a linear search algorithm?

A

O(1)

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

what does time complexity mean?

A

how well does the algorithm scale?
how does the number of steps grow as the input size (n) grows

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

how does a binary search work?

A

the array is spit in half and the middle term is compared to the search item.
if the search item is less than the mid item, the right side of the array is discarded.
if the search item is greater than the mid term, the left side is discarded.
this is repeated until the item is found or there is only 1 item left.

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

what is the condition for a binary search?

A

the array must be sorted

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

what is the time complexity for a binary search algorithm?

A
  • worst case - the item is not in the array O(logn)
  • average case - O(logn)
  • best case - item is at the midpoint O(1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is the space complexity of a binary search algorithm?

A

O(1)

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

how does a bubble sort work?

A

the first two terms are compared in size. if the second term is less than the first term, they are swapped.
this is done until the end of the array is reached.
this is one parse.
there are multiple parses to sort the array and a final parse to check if the array is sorted.

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

what is the time complexity of a bubble sort algorithm?

A
  • worst case - if the array is in reverse order O(n²)
  • average case - O(n²)
  • best case - the array is already sorted O(1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is the space complexity of a bubble sort algorithm?

A

O(1)

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

how does an insertion sort work?

A

the second term is compared to the one to the left of it. if it is less, the item on the left is shifted towards the right. this is continued until the item is inserted into the correct position

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

what is the time complexity of an insertion sort algorithm?

A

worst case -

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

what is the space complexity of an insertion sort algorithm?

A

O(1)

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

how does a merge sort algorithm work?

A
  • the array is split into two left and right sublists
  • a merge sort is performed on the left and right sublists
  • the sublists are split in half until they are one item
  • the items are then merged together into sublists that are sorted
  • this is repeated until the list is sorted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is the time complexity of a merge sort algorithm?

A

O(nlogn)

17
Q

what is the space complexity of a merge sort algorithm?

A

O(n)

18
Q

why is the space complexity of a merge sort algorithm what it is?

A

the space complexity = O(n)
this is because the set of variables will be created n times each time the merge sort is recursively called

19
Q

how does a quick sort algorithm work?

A
  • choose an index to be the pivot
  • put the items in the array that are less than the pivot on the left
  • put the items in the array that are greater than the pivot on the right
  • perform quick sorts on the left and right sublists
  • repeat until all items have been a pivot
20
Q

what is the time complexity of a quick sort algorithm?

A

worst case - the items after the pivot are all less than or all greater than the pivot O(n²)
average case - O(nlogn)
best case - O(nlogn)

21
Q

what is the space complexity of a quick sort algorithm?

A

O(logn)