y9 Algorithms: Linear Search, Binary Search, Bubble Sort, Insertion Sort
Terms in this set (21)
When is a linear search used?
To search the items in a file or an array in memory, if they are in no particular order/sequence
What does a linear search do?
Searches the data items one by one until the required item is found or the end of the list is reached
What happens in a linear search?
1. Set up the search criteria
2. Examine first item in the data set
3. If there is a match, end the procedure and return the result with 'match found'
4. If no match is found repeat with the next item
5. If the last item is reached and no match is found return 'match not found'.
pseudocode algorithm for linear search
function linearSearch(list, item)
-index = - 1
-curritem = 0
-while curritem < len(list) and index = - 1
--if list(curritem) = item then
--index = curritem
-curritem = curritem + 1
What is a binary search?
Searches a list for an item like linear, but only works if the list is sorted. Otherwise, it can only be searched with a linear search.
How does it work?
It repeatedly divides in half the portion of the list data that could contain the required data item, continued until there is only one item in the list.
Example of binary search
A list goes 15, 21, 29, 32, 34, 40, 42, 43, 49, 50, 60, 64, 66, 71, 74, 77
Stage 1: We are looking for 50. The middle term is 43 (as the middle item of an even number of items is found by rounding down), so we can discard all data items less than or equal to 43
Stage 2: the list is now 49, 50, 60, 64, 66, 71, 74, 77. The middle term is 64, so we can discard all data items greater than or equal to 64
Stage 3: the list is now 49, 50, 60. The middle term is 50- so we have found the data item
What happens in the binary search algorithm
-The ordered array is divided into three parts: a middle item, the first part of the array starting at aList up to the middle item, and the second part starting after the middle item and ending after the final item in the list.
-The middle item is examined to see if it is equal to the sought item
-If it is not, then if the middle item is greater than the sought item, the second half of the array is of no further interest
-So the number of items is therefore halved and the process is repeated until the last item is examined, with either the first or second half of the try of items being eliminated at each pass
-'first', 'last' and 'midpoint' are integer variables used to index elements of the array
-the variable 'first' will start at 0, the beginning of the array
-the variable 'last' starts at 'len(aList) - 1, the last array index
Binary Search Pseudocode
found = False
length = list.length
first = list(0)
last = length - 1
while found = False
-midpoint = first + last / 2
-if list[midPoint] = item then
--print('Found at' , midPoint)
--found = True
-elseif list[midPoint] > item then
--last = midpoint - 1
--first = midpoint + 1
if found == False then
Short version of what happens
-pointers at the start and end of the list
-midpoint is found
-check item against midpoint
-make pointer at the end = midpoint - 1 if the value at the midpoint is greater, + 1 if less
Is a linear search always less efficient than a binary search?
No, because the value could be at the beginning of the list
What is sorting?
-Very common task in data processing
-Sorting algorithms are used to sort through a number of items to considerably reduce time spent on a task
What is the Bubble Sort?
-one of the most basic sorting algorithms and simplest to understand
-the idea is to bubble up the largest (or smallest) item to the end of the list, then the second largest, then the third largest and so on until no more swaps are needed
-is simple, but inefficient
How does the Bubble Sort work?
-In an array of n items
-Go through an array, comparing each item with the one next to it. If it is greater, then swap them
-The last element of the array will be in the correct place after the first pass
-Repeat n-2 times, reducing by one on each pass the number of elements to be examined
How can a bubble sort be made more efficient?
-by not counting to the top each time but after each iteration assuming the end of the list is sorted
-by including a flag for swaps in each iteration- if no swaps are made on an iteration then the list is assumed to be in order
How efficient is a bubble sort compared to an insertion sort?
-On paper, the same. In reality, as the size of the data set increases, the insertion sort becomes more efficient.
-This is because in an insertion sort, the iteration stops when the correct position to insert is found.
Example of Bubble Sort happening
, 4, 15, 3, 6, 11
, 15, 3, 6, 11
, 3, 6, 11
5, 4, 7,
, 6, 11
5, 4, 7, 3,
5, 4, 7, 3, 6,
5, 4, 7, 3, 6, 11, 15
After the first pass, the largest item is in the correct place at the end of the list. On the second pass, only the first six numbers are checked.
Pass 2: 4, 5, 3, 6, 7, 11, 15
11 and 15 are in the correct place, so only the first five numbers are checked
Pass 3: 4, 3, 5, 6, 7, 11, 15
7, 11 and 15 are in the correct place, so only the first four numbers are checked.
Pass 4: 3, 4, 5, 6, 7, 11, 15
6, 7, 11 and 15 are in the correct place, so only the first three numbers are checked
Pass 5: 3, 4, 5, 6, 7, 11, 15
The last two numbers are checked and swapped
In this case, no numbers were swapped on Pass 5. So Pass 6 was not necessary. In order to avoid unnecessary passes on a list that is already in sequence, a flag may be set and tested on each pass so that if no swaps were made, no more unnecessary passes are made through
What is Insertion Sort?
-A sorting algorithm that sorts one data item at a time
-Similar to how you might sort a hand of cards
-More efficient that the bubble sort, but not as efficient as the merge sort or quick sort
How does an insertion sort work?
-requires the sorted position to be tracked
-has an outer loop that moved the sorted potion up until the end of the list
-has an inner loop that starts from 1 'up' from the last sorted item and counts down the sorted portion until it finds the position to insert the next number
-the number to be inserted is saved in a temp variable and then placed in the current location in the list
-the algorithm goes down the sorted portion and swaps items as it goes until it finds the position the item should be at
-This process is repeated until there are no more unsorted data items
Example of Insertion Sort into ascending order
, 5, 4, 15, 3, 6, 11
We leave the first item at the start of the list
1st Pass: 5 is inserted into the sorted list:
, 4, 15, 3, 6, 11
2nd Pass: 4 is inserted into the sorted list:
4, 5, 7
, 15, 3, 6, 11
3rd Pass: 15 is now inserted into the sorted list/it stays where it is:
4, 5, 7, 15
, 3, 6, 11
4th Pass: 3 is inserted into the sorted list:
3, 4, 5, 7, 15
, 6, 11
5th Pass: 6 is inserted into the sorted list:
3, 4, 5, 6, 7, 15
6th Pass: 11 is inserted into the sorted list:
3, 4, 5, 6, 7, 15, 11
What is happening in the Insertion Sort example?
-On each pass, the current data item is checked against those already in the sorted list - in the **.
-If the data item being compared in the sorted list is larger than the current data item, it is now shifted to the right
-This continues to happen until we reach a data item in the sorted list which is smaller than the current data item
-e.g. on the 5th pass 6 is compared with 15, and since it is smaller, 15 is shifted right. And 6 is compared with 7, and 7 is shifted right. Then 6 is compared with 5, and it is larger, so it is inserted into the free space.
OTHER SETS BY THIS CREATOR
Programming - Python Review y7
y9 Computing - 2.1b Standard Algorithms (Search/Sort)
THIS SET IS OFTEN IN FOLDERS WITH...