VB Data Structures - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

VB Data Structures

Description:

If X Y, we continue our search confine the search to first half only. If X Y, we continue our search confine the search to second half. RAD 1 - Heriot-Watt Uni. ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 13
Provided by: yus69
Category:

less

Transcript and Presenter's Notes

Title: VB Data Structures


1
VB Data Structures
2
Atomic Variables
  • Atomic variables can only store one value at a
    time.
  • Dim fName As String
  • Dim sName As String
  • Dim age As Integer
  • A value stored in an atomic variable cannot be
    subdivided.

3
Data Structures
  • A data structure is a data type whose value can
    be composed of individual elements.
  • Each element has a data type which can be
  • Atomic.
  • Data structure.

4
Arrays Re-visited
  • An array is a special kind of data structure.
  • It stores a list of related values of the same
    type.
  • It is a homogeneous data structure.
  • Such values can be accessed using a single
    variable name.
  • Dim myArray(6) As Integer
  • Creates an array containing --- elements, stored
    at indexes -----------

5
Heterogeneous Data Structures
  • Sometimes, we want to store values of different
    data types in one place.
  • Cant use arrays.
  • A data structure that can store different types
    in one variable is called a record.

6
Structure Definition
  • Structure Person
  • Dim fName As String
  • Dim sName As String
  • Dim age As Integer
  • End Structure
  • The above definition creates a new data type
    named Person.
  • It defines three structure members fName, sName,
    age.

7
Using Structures
  • To use a structure, a variable need to be
    declared having the structure data type.
  • Dim person1 As Person
  • Dim family(4) As Person
  • To access structure members, use variable name
    and member name, separated by a period.
  • person1.fName yussuf
  • family(1).age 50

8
Search a List Data StructureLinear Search
  • For each item in the list
  • Check to see if the item you're looking for
    matches the item in the list.
  • If it matches.
  • Return the location where you found it (the
    index).
  • End of loop
  • If we get here, we know the item does not exist
    in the list.
  • Return -1.

9
Linear Search Function
  • Public Function linearSearch(ByVal persons() As
    Person, ByVal size As Integer, ByVal key As
    String) As Integer
  • Dim index As Integer
  • For index 0 To size
  • if( String.compare(
    persons(index).sName, key) 0) Then
  • Return index
  • End If
  • Next
  • Return -1
  • End Function
  • Dim sample(2) As Person
  • Dim match As Integer -2
  • sample(0).sName "smith"
  • sample(1).sName "hillman"
  • sample(2).sName "McDonald"
  • match linearSearch(sample, 2, hillman")

10
Binary Search
  • List must be sorted.
  • If x is the value we are looking for
  • Compare X to the middle value of the list. If
    XY, (the middle element) we are done, return Ys
    index. If X lt Y, we continue our search confine
    the search to first half only. If X gt Y, we
    continue our search confine the search to second
    half.

11
Binary Search Function
  • Public Function binarySearch(ByVal list() As
    Integer, ByVal size As Integer, ByVal key As
    Integer) As Integer
  • Dim left, right, middle As Integer
  • left 0
  • right size
  • Do While left ltright
  • middle Int( (left right) / 2)
  • if key list(middle) Then
  • Return middle
  • ElseIf key gt list(middle) Then
  • left middle 1
  • Else
  • right middle - 1
  • End If
  • Loop
  • Return -1
  • End Function

12
Binary Search Test
  • Dim sample(4) As Integer
  • Dim match As Integer -1
  • sample(0) 2
  • sample(1) 5
  • sample(2) 7
  • sample(3) 10
  • sample(4) 12
  • match binarySearch(sample, 4, 5)
Write a Comment
User Comments (0)
About PowerShow.com