0% found this document useful (0 votes)
9 views4 pages

Sorting Searching

The document contains Visual Basic programs for sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort) and searching algorithms (Binary Search, Linear Search). Each program includes procedures for reading input, executing the sorting or searching, and displaying results. The code snippets demonstrate how to manipulate arrays and implement these algorithms in a Visual Basic environment.

Uploaded by

Anik Patra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Sorting Searching

The document contains Visual Basic programs for sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort) and searching algorithms (Binary Search, Linear Search). Each program includes procedures for reading input, executing the sorting or searching, and displaying results. The code snippets demonstrate how to manipulate arrays and implement these algorithms in a Visual Basic environment.

Uploaded by

Anik Patra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Visual Basic Programs

Program for Sorting Algorithms:


Dim a(20) As Integer
Dim n As Integer

‘Procedure for Reading The array


Private Sub Command1_Click()
Dim i As Integer
n = Val([Link])
For i = 0 To n - 1
a(i) = Val(InputBox("Enter the " & (i + 1) & "th element", "Input"))
Next
For i = 0 To n - 1
[Link] a(i)
Next
End Sub

‘Procedure for Bubble Sort


Private Sub Command1_Click()
Do While i < n
j=0
Do While j < n - i - 1
If a(j) > a(j + 1) Then
tmp = a(j)
a(j) = a(j + 1)
a(j + 1) = tmp
j=j+1
End If
Loop
i=i+1
Loop
For i = 0 To n - 1
[Link] = [Link] & a(i) & ", "
Next
End Sub

1
Visual Basic Programs

‘Procedure for Insertion Sort


Private Sub Command2_Click()
Dim i As Integer
Dim j As Integer
Dim tmp As Integer
[Link]
For i = 1 To n - 1
tmp = a(i)
j=i-1
Print j
Do While (j >= 0 And tmp < a(j))
a(j + 1) = a(j)
a(j) = tmp
j=j-1
Loop
Next i
[Link] = "After insertion sort"
For i = 0 To n - 1
[Link] a(i)
Next
End Sub

‘Procedure for Selection Sort


Private Sub Command3_Click()
Dim i As Integer
Dim j As Integer
Dim tmp As Integer
Dim min As Integer
Dim pos As Integer
For i = 0 To n - 1
min = a(i)
pos = i
For j = i + 1 To n - 1
If a(j) < min Then
min = a(j)
pos = j
End If
Next j
tmp = a(i)
a(i) = min
a(pos) = tmp
Next i
[Link]
[Link] = "After selection sort"
For i = 0 To n - 1
[Link] a(i)
Next
End Sub
2
Visual Basic Programs

Program for Binary Search:


Dim arr(10) As Integer
Dim i, n, item, flag As Integer
Dim str As String

Private Sub Command1_Click()


n = Val([Link])
For i = 0 To n - 1
arr(i) = Val(InputBox("Enter the value for element " & i + 1))
Next

For i = 0 To n - 1
[Link] = [Link] & arr(i) & ", "
Next
End Sub

Private Sub Command2_Click()


Dim ub, lb, mid As Integer
flag = 0
lb = 0
ub = n - 1
item = Val([Link])
Do
mid = (ub + lb) / 2
If item = arr(mid) Then
flag = 1
Exit Do
End If
If item > arr(mid) Then
lb = mid + 1
Else
ub = mid - 1
End If
Loop While (lb <= ub)

If flag = 0 Then
str = MsgBox("The item is not found", vbCritical)
Else
str = MsgBox("The item is found", vbInformation)
End If
End Sub

3
Visual Basic Programs

Program for Linear Search:


Dim arr(10) As Integer
Dim i, n, item, flag As Integer
Dim str As String
Private Sub Command1_Click()
item = Val([Link])
flag = 0
For i = 0 To n - 1
If arr(i) = item Then
flag = i + 1
End If
Next

If flag = 0 Then
str = MsgBox("The item is not found", vbCritical)
Else
str = MsgBox("The item is found at position " & flag, vbInformation)
End If
End Sub

Private Sub Command2_Click()


n = Val([Link])
For i = 0 To n - 1
arr(i) = Val(InputBox("Enter the value for element " & i + 1))
Next

For i = 0 To n - 1
[Link] = [Link] & arr(i) & ", "
Next

End Sub

Private Sub Form_Load()

End Sub

You might also like