CSCI 3327 Visual Basic Chapter 7 Data Manipulation
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays UTPA – Fall 2011
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 2
In the Last Class: Example of an Array • Array: C – Dim C = New Integer(11) C(0) -45 C(1) 6 C(2) 0 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 3
Sort an Array C(0) -45 C(0) -1345 C(1) 6 C(1) -45 C(2) 0 C(3) 72 C(3) 6 C(4) 34 C(4) 10 C(5) 39 C(5) 33 C(6) 98 C(6) 34 C(7) -1345 C(7) 39 C(8) 939 C(8) 40 C(9) 10 C(9) 72 C(10) 40 C(10) 98 C(11) 33 C(11) 939 4
Class Array • Array is a class in namespace System – Method for sorting: Array. Sort(array. Name) 5
Example 7. 12: Sort. Array. vb • URL: – http: //media. pearsoncmg. com/ph/esm/deitel/vb_ht p_2010/codeexamples. html • List. Box – Original. Values. List. Box. Items. Clear() • Button – sort. Button. Enabled = True • Array. Sort(integer. Array) 6
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: Customer. Name 7
Bubble Sort Dim sorted As Boolean = False Dim j As Integer = Customer. Name. Get. Upper. Bound(0) While Not (sorted) sorted = True For i = 0 To j - 1 If Customer. Name(i) > Customer. Name(i + 1) Then Swap(i) sorted = False End If Next i j=j-1 End While 8
Swap Private Sub Swap(By. Val i As Integer) Dim tmp 1 As String Dim tmp 2 As String tmp 1 = Customer. Name(i) = Customer. Name(i + 1) = tmp 1 tmp 2 = Customer. Tele(i) = Customer. Tele(i + 1) = tmp 2 End Sub 9
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 10
Linear Search search key C(0) -45 C(1) 6 C(2) 0 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 11
Example 7. 14: Linear. Search. Test. vb • URL: – http: //media. pearsoncmg. com/ph/esm/deitel/vb_ht p_2010/codeexamples. html • Code: – If search. Data(i) = search. Key Then – index = i – Exit For 'terminate the loop, as the key is found – End If 12
Binary Search search key: 10 C(0) -1345 C(1) -45 C(2) 0 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 middle last 13
Binary Search search key: 10 C(0) -1345 C(1) -45 C(2) 0 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 middle last 14
Binary Search search key: 10 C(0) -1345 C(1) -45 C(2) 0 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 middle last 15
Binary Search search key: 10 C(0) -1345 C(1) -45 C(2) 0 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 middle last 16
Code of Binary Search Dim Lookfor As String Dim first, last, middle As Integer Dim nfound As Boolean Lbl. Name. Caption = "": Lbl. Tele. Caption = "" Lookfor = Txt. Search. Text nfound = False first = 1: last = Index While (Not (nfound) And first <= last) middle = (first + last) 2 If RTrim(Lookfor) = RTrim(Customer. Name(middle)) Then nfound = True Lbl. Name. Caption = Customer. Name(middle) Lbl. Tele. Caption = Customer. Tele(middle) Else. If Lookfor < Customer. Name(middle) Then last = middle - 1 Else: first = middle + 1 End If End While If Not (nfound) Then Lbl. Name. Caption = "Name not in list" 17
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 0 th location for data, then you can say it is empty when stack. top is zero 18
Rectangular Array • So far, we have studied one-dimensional array – Dim C = New Integer (11) • Rectangular array is a 2 -dimensional array Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 3 a(0, 0) a(1, 0) a(2, 0) a(0, 1) a(1, 1) a(2, 1) a(0, 2) a(1, 2) a(2, 2) a(0, 3) a(1, 3) a(2, 3) 19
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}} 20
Case Study: Grade Report Using a Rectangular Array • Example 7. 20: Grade. Report. vb • URL: – http: //media. pearsoncmg. com/ph/esm/deitel/vb_ht p_2010/codeexamples. html 21
Resize an Array • Keyword: Re. Dim • 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} – Re. Dim values(6) ' {0, 0, 0, 0} – Re. Dim Preserve values(6) ' {1, 2, 3, 4, 5, 0, 0} 22
23
- Slides: 23