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
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
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
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
Class Array Array is a class in namespace System Method for sorting: Array.Sort(arrayName)
Example 7.12: SortArray.vb URL: ListBox Button http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html ListBox OriginalValuesListBox.Items.Clear() Button sortButton.Enabled = True Array.Sort(integerArray)
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
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
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
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
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
Example 7.14: LinearSearchTest.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html Code: If searchData(i) = searchKey Then index = i Exit For 'terminate the loop, as the key is found End If
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
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
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
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
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"
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
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
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}}
Case Study: Grade Report Using a Rectangular Array Example 7.20: GradeReport.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html
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}