Wednesday, December 8, 2010

lecture notes

sorted array
linear search - can speed up; we can end prematurely
binary search

search algorithm

why "Or n = 9"?
do not want OutOfArrayBounds exception

  Dim name2Find As String
  Dim n As Integer = -1
  name2Find = txtName.Text.ToUpper
  ' start as pessimist
  txtResult.Text = "Not found."
'  for i = 0 to 9
'    if nom(i) = name2Find then...
'  next
  For Each x in nom
    if x = name2Find Then
      txtResult.Text = "Found."
    Elseif x = name2Find Then
      exit for
    Endif
  Next

' that was my version of linear search as found in book
 Dim list1() As String = {"Al", "Carl", "Don", "Greg", "Jack"}
  Dim list2() As String = {"Bob", "Carl", "Eric", "Greg", "Herb"}

to merge, I COULD first copy contents of both, and then sort
Bad idea!

do while ia <= Ubound(a) and ib <= Ubound(B)
if A(ia) <= B(ib) Then
C(ic) = A(ia)
ia+= 1
else
C(ic) = B(ib)
ib +=1
endif
ic +=1
loop
for ia = ia to Ubound(A)
   C(ic) = A(ia)
   ic += 1
next
for ib = ib to Ubound(B)
   C(ic) = A(ib)
   ic += 1
next

No comments:

Post a Comment