Visual basic code p4
Linear searching using for loop
Sub linearsearch_for_loop()
Dim data() As Integer = {22, 66, 55, 88, 99}
Dim found As Boolean = False
Dim i, searchno, indexnum As Integer
searchno = Console.ReadLine
For i = 0 To data.Length - 1
If searchno = data(i) Then
found = True
indexnum = i
Exit For
End If
Next
If found = True Then
Console.WriteLine("number found at " & indexnum)
Else
Console.WriteLine("number not found")
End If
End Sub
Linear searching using While loop
Sub linearsearch_while_loop()
Dim data() As Integer = {22, 66, 55, 88, 99}
Dim found As Boolean = False
Dim i, searchno, indexnum As Integer
searchno = Console.ReadLine
i=0
While i <= data.Length - 1 And found = False
If searchno = data(i) Then
found = True
indexnum = i
End If
TAHA IQBAL
, i=i+1
End While
If found = True Then
Console.WriteLine("number found at " & indexnum)
Else
Console.WriteLine("number not found")
End If
End Sub
Linear searching using Repeat loop
Sub linearsearch_repeat_loop()
Dim data() As Integer = {22, 66, 55, 88, 99}
Dim found As Boolean = False
Dim i, searchno, indexnum As Integer
searchno = Console.ReadLine
i=0
Do
If searchno = data(i) Then
found = True
indexnum = i
End If
i=i+1
Loop Until (i > data.Length - 1 Or found = True)
If found = True Then
Console.WriteLine("number found at " & indexnum)
Else
Console.WriteLine("number not found")
End If
End Sub
TAHA IQBAL
, Bubble Sort(Efficient)
Sub BubbleSort()
Dim Temp As String
Dim NoSwaps As Boolean
Dim Boundary, J As Integer
Boundary = 8
Do
NoSwaps = True
For J = 0 To Boundary
If Contact(J) > Contact(J + 1) Then
Temp = Contact(J)
Contact(J) = Contact(J + 1)
Contact(J + 1) = Temp
NoSwaps = False
End If
Next
Boundary = Boundary - 1
Loop Until NoSwaps = True
End Sub
Binary searching
Sub Main()
Dim data() As Integer = {33, 45, 55, 66, 77}
Dim lowerindex, highestindex, middleindex, searchindex,
searchno As Integer
Dim found As Boolean = False
lowerindex = 0
highestindex = data.Length - 1
searchno = Console.ReadLine
While highestindex >= lowerindex And found = False
middleindex = Int((highestindex + lowerindex) / 2)
If data(middleindex) = searchno Then
found = True
TAHA IQBAL
Linear searching using for loop
Sub linearsearch_for_loop()
Dim data() As Integer = {22, 66, 55, 88, 99}
Dim found As Boolean = False
Dim i, searchno, indexnum As Integer
searchno = Console.ReadLine
For i = 0 To data.Length - 1
If searchno = data(i) Then
found = True
indexnum = i
Exit For
End If
Next
If found = True Then
Console.WriteLine("number found at " & indexnum)
Else
Console.WriteLine("number not found")
End If
End Sub
Linear searching using While loop
Sub linearsearch_while_loop()
Dim data() As Integer = {22, 66, 55, 88, 99}
Dim found As Boolean = False
Dim i, searchno, indexnum As Integer
searchno = Console.ReadLine
i=0
While i <= data.Length - 1 And found = False
If searchno = data(i) Then
found = True
indexnum = i
End If
TAHA IQBAL
, i=i+1
End While
If found = True Then
Console.WriteLine("number found at " & indexnum)
Else
Console.WriteLine("number not found")
End If
End Sub
Linear searching using Repeat loop
Sub linearsearch_repeat_loop()
Dim data() As Integer = {22, 66, 55, 88, 99}
Dim found As Boolean = False
Dim i, searchno, indexnum As Integer
searchno = Console.ReadLine
i=0
Do
If searchno = data(i) Then
found = True
indexnum = i
End If
i=i+1
Loop Until (i > data.Length - 1 Or found = True)
If found = True Then
Console.WriteLine("number found at " & indexnum)
Else
Console.WriteLine("number not found")
End If
End Sub
TAHA IQBAL
, Bubble Sort(Efficient)
Sub BubbleSort()
Dim Temp As String
Dim NoSwaps As Boolean
Dim Boundary, J As Integer
Boundary = 8
Do
NoSwaps = True
For J = 0 To Boundary
If Contact(J) > Contact(J + 1) Then
Temp = Contact(J)
Contact(J) = Contact(J + 1)
Contact(J + 1) = Temp
NoSwaps = False
End If
Next
Boundary = Boundary - 1
Loop Until NoSwaps = True
End Sub
Binary searching
Sub Main()
Dim data() As Integer = {33, 45, 55, 66, 77}
Dim lowerindex, highestindex, middleindex, searchindex,
searchno As Integer
Dim found As Boolean = False
lowerindex = 0
highestindex = data.Length - 1
searchno = Console.ReadLine
While highestindex >= lowerindex And found = False
middleindex = Int((highestindex + lowerindex) / 2)
If data(middleindex) = searchno Then
found = True
TAHA IQBAL