Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays

Similar presentations


Presentation on theme: "CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays"— Presentation transcript:

1 CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous lecture slides. – Xiang Lian

2 Objectives In this chapter, you will: Learn sorting data in arrays
Sort method of arrays Linear search Binary search Learn the declaration of rectangular arrays Know how to resize an array in Visual Basic

3 In the Last Class: Example of an Array
Array: C Dim C = New Integer(11) C(0) -45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33

4 Sort an Array C(0) -45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39 C(6) 98
C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33 C(0) -1345 C(1) -45 C(2) C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939

5 Class Array Array is a class in namespace System
Method for sorting: Array.Sort(arrayName)

6 Example 7.12: SortArray.vb URL: ListBox Button
ListBox OriginalValuesListBox.Items.Clear() Button sortButton.Enabled = True Array.Sort(integerArray)

7 Sorting and Searching Sorting implementation
You need to implement it Don’t use the method Sort comes with array class You may use bubble sort or selection sort Assume we have an array: CustomerName

8 Bubble Sort Dim sorted As Boolean = False
Dim j As Integer = CustomerName.GetUpperBound(0) While Not (sorted) sorted = True For i = 0 To j - 1 If CustomerName(i) > CustomerName(i + 1) Then Swap(i) sorted = False End If Next i j = j - 1 End While

9 Swap Private Sub Swap(ByVal i As Integer) Dim tmp1 As String
tmp1 = CustomerName(i) CustomerName(i) = CustomerName(i + 1) CustomerName(i + 1) = tmp1 tmp2 = CustomerTele(i) CustomerTele(i) = CustomerTele(i + 1) CustomerTele(i + 1) = tmp2 End Sub

10 Searching Searching: to determine whether a value (called search key) is in the array Linear search Compare each element of the array with the search key For either sorted or unsorted array Efficient for small array Binary search For sorted array

11 Linear Search search key C(0) -45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39
C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33

12 Example 7.14: LinearSearchTest.vb
URL: Code: If searchData(i) = searchKey Then index = i Exit For 'terminate the loop, as the key is found End If

13 Binary Search first search key: 10 middle last C(0) -1345 C(1) -45
C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939 first search key: 10 middle last

14 Binary Search first search key: 10 middle last C(0) -1345 C(1) -45
C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939 first search key: 10 middle last

15 Binary Search first search key: 10 middle last C(0) -1345 C(1) -45
C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939 first search key: 10 middle last

16 Binary Search first search key: 10 middle last C(0) -1345 C(1) -45
C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939 first search key: 10 middle last

17 Code of Binary Search Dim Lookfor As String
Dim first, last, middle As Integer Dim nfound As Boolean LblName.Caption = "": LblTele.Caption = "" Lookfor = TxtSearch.Text nfound = False first = 1: last = Index While (Not (nfound) And first <= last) middle = (first + last) \ 2 If RTrim(Lookfor) = RTrim(CustomerName(middle)) Then nfound = True LblName.Caption = CustomerName(middle) LblTele.Caption = CustomerTele(middle) ElseIf Lookfor < CustomerName(middle) Then last = middle - 1 Else: first = middle + 1 End If End While If Not (nfound) Then LblName.Caption = "Name not in list"

18 Using an Array for a Stack
Keep track of the top Push: Increase it before adding Pop: Decrease it after taking Push or Pop only to or from the top Examine the stack to see if it is empty or full For example, if stack.top < 0, then it is empty You may choose not to use the 0th location for data, then you can say it is empty when stack.top is zero

19 Rectangular Array So far, we have studied one-dimensional array
Dim C = New Integer (11) Rectangular array is a 2-dimensional array Column 0 Column 1 Column 2 Column 3 a(0, 0) a(0, 1) a(0, 2) a(0, 3) a(1, 0) a(1, 1) a(1, 2) a(1, 3) a(2, 0) a(2, 1) a(2, 2) a(2, 3) Row 0 Row 1 Row 2

20 Declaration of Rectangular Array
Create a 2-by-2 array Dim numbers (1, 1) As Integer Initialize values numbers (0, 0) = 1 numbers (0, 1) = 2 numbers (1, 0) = 3 numbers (1, 1) = 4 Declare and initialize a rectangular array Dim numbers = {{1, 2}, {3, 4}} Dim numbers(,) As Integer = {{1, 2}, {3, 4}}

21 Case Study: Grade Report Using a Rectangular Array
Example 7.20: GradeReport.vb URL:

22 Resize an Array Keyword: ReDim An array's size cannot be changed
But a new array can be created and assigned to an array variable Example: Dim values() As Integer = {1, 2, 3, 4, 5} ReDim values(6) ' {0, 0, 0, 0, 0, 0, 0} ReDim Preserve values(6) ' {1, 2, 3, 4, 5, 0, 0}

23


Download ppt "CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays"

Similar presentations


Ads by Google