19.1 Algorithm Searchinmg and sorting
19.1 Algorithm Searchinmg and sorting
Syllabus Content:
19.1 Algorithms part:1 (Linear and Binary search, Bubble sort & insertion sort)
Linear Search:
Linear search is a method of searching a list in which each element of an array is
checked in order, from the lower bound to the upper bound, until the item is found, or
the upper bound is reached.
OUTPUT
OUTPUT
Binary Search:
A binary search is most efficient if List is already sorted.
The value of the middle item in the list is first tested to check if it matches the required item,
and half of the list that does not contain the item is then discarded.
Then in next step, value is again checked from the middle in remaining half of list and if not
found again half of list is discarded. This is repeated until the item is found or nothing is left in
List to check.
Binary search takes far few comparisons compared to Linear search which checks each and
every item one by one.
DECLARE mylist() As Integer = {11, 22, 33, 44, 55, 66, 77, 88, 99, 110}
DECLARE upperbound, lowerbound, index, item : INTEGER
DECLARE found : BOOLEAN
upperbound = 10
lowerbound = 0
found = False
OUTPUT ("please input num to be found")
INPUT item
REPEAT
index = Int((upperbound + lowerbound) / 2)
IF item = mylist(index) THEN
found = True
ELSEIF item > mylist(index) THEN
lowerbound = index + 1
ELSE upperbound = index - 1
END IF
UNTIL (found = True) OR upperbound = lowerbound
IF found = True THEN
OUTPUT ("Item found: " & item)
ELSE
OUTPUT ("Item not found")
END IF
Bubble Sort
When we have completed the first pass through the entire array, the largest value is in the
correct position at the end of the array. The other values may or may not be in the correct order.
We need to work through the array again and again. After each pass through the array the next
largest value will be in its correct position, as shown in Figure below.
In effect we perform a loop within a loop, a nested loop. This method is known as a bubblesort.
The name comes from the fact that smaller values slowly rise to the top, like bubbles in a liquid.
Do
Swap = False
'LOOP can work fine without STEP also
For index = 0 To top - 1 Step 1 'STEP is a keyword to increment in loop
If myList(index) > myList(index + 1) Then
temp = myList(index)
myList(index) = myList(index + 1)
myList(index + 1) = temp
Swap = True
End If
Next
top = top - 1
Loop Until (Not Swap) Or (top = 0)
'Outpout The Sorted Array
For index = 0 To myList.Length - 1
Console.WriteLine(myList(index) & " ")
Next
Console.ReadKey()
End Sub
End Module
Sub Main()
Console.WriteLine("Bubble Sorting")
Console.WriteLine()
Dim num, count As Integer
Console.Write("Enter Number of Elements: ")
num = CInt(Console.ReadLine)
Dim array(num) As Integer 'Array Made to insert values to be sorted
Console.WriteLine()
Console.WriteLine()
Console.WriteLine("Inputted Elements")
Console.WriteLine()
Console.WriteLine()
Output:
Insertion sort:
Imagine you have a number of cards with a different value printed on each card. How would you
sort these cards into order of increasing value?
You can consider the pile of cards as consisting of a sorted part and an unsorted part. Place the
unsorted cards in a pile on the table. Hold the sorted cards as a pack in your hand. To start with
only the first (top) card is sorted. The card on the top of the pile on the table is the next card to
be inserted. The last (bottom) card in your hand is your current card.
Figure shows the sorted cards in your hand as blue and the pile of unsorted cards as white. The
next card to be inserted is shown in red. Each column shows the state of the pile as the cards
are sorted.
DECLARE myList : ARRAYS [ ] OF INTEGER = {70, 46, 43, 27, 57, 41, 45, 21, 14}
DECLARE upperBound, lowerBound, index, key, place, temp : INTEGER
upperBound LENGTH(myList())
lowerBound 0
top upperBound
DECLARE mylist() As Integer = {70, 46, 43, 27, 57, 41, 45, 21, 14}
DECLARE count As Integer 'to access array from left to right
DECLARE current As Integer ' to hold the current item to find place in sorted list
DECLARE position As Integer 'to track from left to right through the sorted list
DECLARE lowerbound As Integer = 0
DECLARE upperbound As Integer = LENGTH(myList())
FOR count = (lowerbound + 1) To upperbound 'starts with second item in array
current = mylist(count)
position = count
WHILE position >= 0 And mylist(position - 1) > current 'Loop Line where VB tries to
access position -1 in array
mylist(position) = mylist(position - 1)
position = position - 1
If position = 0 Then 'This code stops VB to go to postion (-1) in array
Exit While
End If
End While
mylist(position) = current
Next
myList = [4,46,43,27,57,41,45,21,14]
lowerBound = 0
upperBound = len(myList)
for index in range(lowerBound + 1, upperBound):
key = myList[index]
place = index -1
if myList[place] > key:
while place >= lowerBound and myList[place] > key:
temp = myList[place + 1]
myList[place + 1] = myList [place]
myList[place] = temp
place = place -1
myList[place + 1] = key
#output the sorted array
print(myList)
myList = [4,46,43,27,57,41,45,21,14]
lowerBound = 0
upperBound = len(myList)
for index in range(lowerBound + 1, upperBound): #Loops
starts at 2nd value
key = myList[index] #holds the next value
place = index -1 #holds the previous value
if myList[place] > key:
while place >=lowerBound and myList[place]> key:
temp = myList[place + 1]
myList[place + 1] = myList [place]
myList[place] = temp
place = place -1
myList[place + 1] = key
#output the sorted array
print(myList)
OUTPUT:
As the number of elements in List increases, the time taken to sort the list increases. It has been
observed that when number of items in list increases, the performance of bubble sort
deteriorates faster than insertion sort
References:
Computer Science AS & A Level Coursebook by Sylvia Langfield & Dave Duddell
Computer Science Teacher’s Resource
Computer Science AS & A level by HODDER EDUCATION
https://www.youtube.com/watch?v=I-kosUr1jtE
https://www.dotnetperls.com/dictionary-vbnet
http://www.worldbestlearningcenter.com/index_files/vb.net-example-insertion-sort.htm