# Searching and sorting algorithms Flashcards

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

When is a linear search used?

A

It is used to search a list of a list when the items are unsorted

2
Q

How is an item found in a linear search?

A

Starting with the first item in the list, it checks whether this is the item it’s searching for, if not it checks the next item. It does this until the item is found or until it reaches the end of the list.

3
Q

What is the maximum number of items that would have to be examined to find a specific item in a linear search of a million items?

A

Maximum number would be 1 million searches

4
Q

What is the average number of items that would have to be examined to find a specific item in a linear search of a million items?

A

Half so 500,000 (five hundred thousand)

5
Q

What is the time complexity of a linear search?

A

In the worst case scenario, it’s O(n)

6
Q

What is the most efficient type of search algorithm and why?

A

A binary search is more efficient because it has a faster time complexity.

7
Q

How is an item found in a binary search?

A
• Find the middle item in the list, if this is the midpoint stop
• If not, compare the sought after item with the middle item. If it comes before the middle item, discard the second half of the list. If it comes after the middle item, discard the first half of the list
• Repeat the first three steps on this smaller list until the sought after item is found.
8
Q

When is a binary search used?

A

When the list is sorted

9
Q

What is a binary search an example of?

A

A divide and conquer algorithm

10
Q

What is the time complexity of a binary search?

A

It’s O(logn)

11
Q

What 3 variables are used in binary searches?

A

First, last and midpoint

12
Q

How is the maximum number of nodes that need to be searched determined in a binary tree search?

A

The maximum number of nodes that need to be traversed depends on the amount of layers in the tree traversal.
If there are 4 layers (including the root node) the maximum number of nodes that need to be traversed is 4

13
Q

What are the steps of a bubble sort for an array of n items?

A
• Go through the array comparing each item with the one next to it. If it is greater, swap them.
• The last item in the array will be in the correct place after the first pass.
• Repeat the first step n-1 times reducing the number of items to be examined by one on each pass
14
Q

Which is the simplest sort and search algorithms to code

A
• Linear search algorithm

- Bubble sort algorithm

15
Q

What is the time complexity of a bubble sort?

A

It’s O(n^2) due to its use of a nested for loop

16
Q

What is a merge sort an example of?

A

A divide and conquer algorithm

17
Q

When is a merge sort used?

A

When there is a large amount of items in the list

18
Q

What are the steps of a merge sort for an array of n items?

A
• Divide the list into n sublists each containing one element
• Repeatedly merge sublists to produce new sorted sublists (by comparing the items in each sublist) until there is only one sublist remaining. This is the sorted list
19
Q

What is the time complexity of a merge sort?

A

There are n sublists to be merged so it has a time complexity : O(nlogn)

20
Q

What is space complexity?

A

The amount of resources such as memory that an algorithm requires.

21
Q

What are the two things that are considered when determining the efficiency of an algorithm?

A

Time complexity

Space complexity