Searches And Sorts (Ch5) (M2) Flashcards
What is a linear search?
A linear search is an algorithm used to perform a search for data in a set of data items. It involves sequentially searching through a list from start to finish comparing values.
Describe the process of a linear search
Process of a linear search:
- Find out the length of the data set
- Set the counter to 0
- Look at the first item in the unordered list
- Check to see if the value at that position matches the value searched for
- If it matches, the value is found. End the search.
- If not, increment the counter by 1 and go back to step 3 until there are no more items to search.
What is a disadvantage of a linear search?
A linear search can be quite inefficient. Suppose the data set contained 100 items of data, and the item searched for happens to be the last item in the set?
All of the previous 99 items would have to be searched through first.
What is an advantage of a linear search?
The linear searches will work on any data set, whether it is ordered or unordered
Perform a linear search for ‘7’ in the list 2, 5, 8, 7, 4, 3
2 != 7 Not found, go to next index
5 != 7 Not found, go to next index
8 != 7 Not found, go to next index
7 == 7, Value found at position 3
What is a binary search?
A binary search is a more efficient algorithm used to perform a search for data in a set of data items.
It involves a divide and conquer technique using mid points to repeatedly split the data set in half and discard items where the search item cannot be located.
Describe the process of a binary search
Process of a binary search:
- Find the middle item in the ordered list
- If the value is a match, the search ends.
- If the value at the midpoint is less than the value to be found, the list is divided in half. The lower half of the list is ignored and the search keeps to the upper half of the list.
- Otherwise, if the value at the midpoint is greater than the value to be found, the upper half of the list is ignored and the search keeps to the lower half of the list.
- The search moves to the midpoint of the remaining items. Steps 2 through 4 continue until a match is made or there are no more items to be found.
Give an advantage of a binary search
A binary search is a much more efficient algorithm than a linear search.
In an ordered list of every number from 0 to 100, a linear search would take 99 steps to find the value 99.
A binary search would only require seven steps.
Give a disadvantage of a binary search
A binary search can only work if a list is ordered
Complete a binary search for the value 11:
0 1 2 3 4 5 6 7 8
1, 3, 4, 5, 7, 9, 11, 14, 16
The midpoint is found by adding the lowest position to the highest position and dividing by 2.
(8 + 0) / 2 = 4
Check position 4, which has the value 7.
7 is less than 11, so the bottom half of the list (including the midpoint) is discarded.
5 6 7 8
9, 11, 14, 16
The new lowest position is 5.
(8 + 5) / 2 = 6.5, which rounds up to 7.
Position 7 has the value 14, which is greater than 11.
So the top half of the list (including the midpoint) is discarded.
5 6
9, 11
The new highest position is 6.
(6 + 5) / 2 = 5.5, which rounds up to 6.
Position 6 has the value 11, which is a match, so the search ends.
Demonstrate a binary search for the value “Bryan” with the following list:
Bryan, Nick, Philip, Robert, Sam, Thomas
0 1 2 3 4 5
Bryan, Nick, Philip, Robert, Sam, Thomas
(0 + 5) / 2 = 2.5, which rounds to 3.
The right side doesn’t contain Bryan, so those values including the midpoint are discarded.
0 1 2
Bryan, Nick, Philip
(0 + 2) / 2 = 1
The right side doesn’t contain Bryan, so that value and the midpoint are discarded.
Bryan is the only value left, output Bryan found at index 0.
What is a bubble sort?
A bubble sort is an algorithm that is used to sort a dataset of values by means of repeatedly comparing adjacent pairs of values and swapping items into the correct order
Describe the process of a bubble sort
Process of a bubble sort:
1) (Work from left to right)
2) Look at the first two items in the list
3) If element 1 is bigger than element 2, then swap them around
4) Otherwise, do nothing
5) Repeat this for the next elements in the list (2 and 3 and 4, then 4 and 5, etc)
6) If there are no elements left, then you have completed a pass. Then start back at the beginning of the list and repeat again.
Give a disadvantage of the bubble sort
Disadvantages:
It is the slowest to execute
It is an inefficient way to sort a list
Due to it being inefficient, the bubble sort algorithm does not cope well with a large list of items
Give an advantage of the bubble sort
Advantages:
It is a simple algorithm that can be easily implemented on a computer
It’s an efficient way to check if a list is already in order.
Doesn’t use much memory as all the sorting is done using the original list.