If I had this array of numbers:
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
And I wanted to find the number β3β, a linear search would work like this:
We look at the first value, in this case, β8β
We assess if this is the required value, in this case, β3β
If this is not the required value, then we move on to the next value. in this case, β5β
We repeat these 3 steps until the value is found, or we come to the end of the array
Each time we are looking at a new value, this is called a βparseβ.
In this case, 6 parses are needed to find the number β3β with a linear search
If I had this array of numbers:
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
And I wanted to find the number β3β, a binary search would work like this:
Firstly, we need to order the array in ascending order
1 | 2 | 3 | 5 | 6 | 8 | 9 |
---|
We find the middle value of this array by using the formula (n+1)/2, where n is the length of the array, in this case, 7.
For this array, we would be looking at the 4th value of β5β.
If this formula has a result of β¦ .5 add another .5 to this to find the ideal value
Once we have found the middle value, we need to assess whether this value is higher than, lower than or equal to the required value.
If this value is lower than the required value, we remove everything higher than and including this middle value.
If this value is higher than the required value, we remove everything lower than and including this middle value.
In this case, β5β is higher than β3β, so we would remove the upper half of the array like so:
1 | 2 | 3 | 5 | 6 | 8 | 9 |
---|
1 | 2 | 3 |
|
|
|
|
---|
So we are left with:
1 | 2 | 3 |
---|
Next, we use steps 2 and 3 again on this new array
In this case, (n+1)/2 would result in us looking at the 2nd value of β2β.
Again, we assess whether the value is higher than, lower than or equal to the required value
In this case, β2β is lower than the required value so te 1st and 2nd values would be removed
We continue this until we reach the required value
Each time we divide, this is a new parse
In this case, 3 parses are needed to find the number β3β with a binary search
Linear Search | Binary Search |
---|---|
Works quicker on a short array | Works much quicker on a longer array |
Can work on any type of array | The array must be ordered before searching |
If I had this array of numbers:
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
And I wanted to sort this array in ascending order, a bubble sort would work like this:
We look at the 1st and 2nd values, in this case, β8β and β5β
If the value on the left is larger than the value on the right, we swap these. If not, we keep these how they are.
In this case, it would look like this:
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
5 | 8 | 9 | 1 | 6 | 3 | 2 |
---|
Next, we would look at the 2nd and 3rd values, in this case, β8β and β9β.
Again, if the value on the left is larger than the value on the right, we swap these. If not, we keep these how they are.
In this case, it would stay the same
We continue this for each pair of values in the array.
In this case, it would look like this:
5 | 8 | 9 | 1 | 6 | 3 | 2 |
---|
5 | 8 | 1 | 9 | 6 | 3 | 2 |
---|
5 | 8 | 1 | 6 | 9 | 3 | 2 |
---|
5 | 8 | 1 | 6 | 3 | 9 | 2 |
---|
5 | 8 | 1 | 6 | 3 | 2 | 9 |
---|
Then again, we do this whole thing again
In this case, we would go from this:
5 | 8 | 1 | 6 | 3 | 2 | 9 |
---|
To this:
5 | 1 | 6 | 3 | 2 | 8 | 9 |
---|
We keep repeating this until the array is fully ordered.
In this case, it would look like this:
5 | 1 | 6 | 3 | 2 | 8 | 9 |
---|
1 | 5 | 3 | 2 | 6 | 8 | 9 |
---|
1 | 3 | 2 | 5 | 6 | 8 | 9 |
---|
1 | 2 | 3 | 5 | 6 | 8 | 9 |
---|
Then we complete one final parse to check that this is fully ordered correctly.
Each time we sort through the whole array, this is a new parse
In this case, 6 parses are needed to sort this array into ascending order using binary search.
If I had this array of numbers:
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
And I wanted to sort this array in ascending order, a merge sort would work like this:
We want to divide this array into 2 parts, of fairly equal length
In this case, as there is an odd number, one array shall be slightly longer
In this case, this would look like this:
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
Then, we want to split these 2 arrays into another further 2
In this case, this would look like this:
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
We continue this until each value is in its own array
In this case, this would look like this:
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
Once all the values are separated, we want to merge these back together in ascending order. To do this, we merge each pair back together in ascending order.
In this case, this would look like this:
8 | 5 | 9 | 1 | 6 | 3 | 2 |
---|
5 | 8 | 1 | 9 | 6 | 2 | 3 |
---|
We want to merge these again back to larger arrays in ascending order. We do not add one array after one another, but we order these whole small arrays.
In this case, this would look like this:
5 | 8 | 1 | 9 | 6 | 2 | 3 |
---|
1 | 5 | 8 | 9 | 2 | 3 | 6 |
---|
We continue until we finish with one array.
In this case, this would look like this:
1 | 5 | 8 | 9 | 2 | 3 | 6 |
---|
1 | 2 | 3 | 5 | 6 | 8 | 9 |
---|
Bubble Sort | Merge Sort |
---|---|
Works quicker on a short array | Works quicker on a longer array |
Uses up less memory | Uses up lots of memory |