Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays & Strings part 2 More sophisticated algorithms, including sorting.

Similar presentations


Presentation on theme: "Arrays & Strings part 2 More sophisticated algorithms, including sorting."— Presentation transcript:

1 Arrays & Strings part 2 More sophisticated algorithms, including sorting

2 Applications in this show Palindrome Sorting values Sort names

3 An aside on using combo box and strings Recall the car sales project from VB8.ppt? Comboboxes contain their own object type, even if you store integers or strings in them. If you are trying to figure out which array value was selected in a combobox, be a little careful.

4 Using combo box and strings Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenter.Click Dim name As String name = txtname.Text Dim i As Integer Dim j As Integer = combo.SelectedIndex For i = 0 To values.Length - 1 If values(i) = name Then list(i) += prices(j) End If Next lstdisplay.Items.Clear() For i = 0 To list.Length - 1 lstdisplay.Items.Add(values(i) & " " & list(i)) Next txtname.Text = "" End Sub

5 Using combo box and strings Use cbo.selectedindex on the combo box if you need to know which one to access a location in your array. Similarly, you could use cbo.selectedtext or cbo.selectedvalue Don’t use cbo.selectedItem for this purpose, as it does not return the correct datatype.

6 An exercise: Shift array contents to next (higher) index Move array contents one location farther, so 0 th value goes in 1 st place, 1 st place goes into 2 nd place and so on Dim values As Integer() = {10, 20, 30, 40, 50} Dim i As Integer For i = values.Length - 1 To 1 Step -1 values(i) = values(i - 1) Next ‘What should we put in position 0? Values(0)=0

7 Another exercise Shift the values to previous location and put a 0 in the last location instead. So: shift values(1) into values(0),, values(2) into values(1), etc

8 Rotate (up) Dim values As Integer() = {10, 20, 30, 40, 50} Dim I,save As Integer Save=values(values.Length - 1 ) For i = values.Length - 1 To 1 Step -1 values(i) = values(i - 1) Next ‘put old last one into 0 th spot values(0) =save What does the array look like now?

9 Rotate right Exercise. Rotate the array the other direction, so each value goes into the previous location and values(0) goes into the last (4 th ) location.

10 Reverse Arrays already have a defined method called reverse to flip their contents. The next few slides cover palindrome checking. You could also check for a palindrome by first converting the string into a char array

11 Palindrome: spells the same forwards or backwards

12 Palindrome Palindromes are spelled the same forwards and backwards. Why wasn’t “a man a plan a canal panama” a palindrome?

13 Palindrome

14 In button click, go through the string to make sure first char matches the last Next char matches next-to-last, etc. Display result

15 Palindrome Harder exercises: Have your palindrome ignore letter case and blanks, so that “A man a plan a canal Panama” is accepted as a palindrome. VB has methods for strings to convert the string to uppercase or lowercase which simplifies some of the palindrome checking.

16 Sorting Array contents can be sorted There are many ways to sort. Here’s a simple way, called selection sort: We could find the largest, put it in position 0. (Where should we put current item in position 0?) Repeat this, finding 2 nd largest, third, and positioning them correctly.

17 build this interface include try catch for format and indexoutofbounds

18 Sorting from big to small

19 The sort button click Dim i, j, last As Integer ‘count is a field value declared elsewhere last=count-1 ‘last entry in array named values For i = 0 To last j = findBigSub(values, i,last) swap(values, i, j) ‘ need to write this sub to swap entries in the values array Next lstvalues.Items.Clear() For i = 0 To count - 1 lstvalues.Items.Add(values(i)) Next

20 The function to find the largest remaining value Function findBigSub(ByVal v() As Integer, ByVal x As Integer, ByVal c as integer) As Integer Dim i, j As Integer i = x 'assume first is biggest For j = x+1 To c If v(j) > v(i) Then i = j 'if position j holds a bigger value, save position End If Next findBigSub = i ‘return answer End Function

21 Not shown A swap subroutine… You’ll need to send the array and the two positions. Send the array byref, not byval: Private Sub swap(ByRef v() As Integer, ByVal x As Integer, ByVal y As Integer) We mostly wrote the 3 lines of code needed in class

22 Remember…. when finding values or displaying array contents in the previous example, you need to be careful. The entire array may not have been filled. Be sure to use count (or count-1), the current number of things stored in the array, (as the upper bound of your loop), rather than array.length since some part of the array may contain nothing.

23 Other ways to sort Bubble sort: Go all the way through the array comparing elements pair wise, and swapping if they are out of order. You’ll have to do this many times… What’s the best case and worst case and how will you know when you are done?

24 Other ways to sort Insertion sort Move elements from the array into a different array (or even the same one) as follows: Put each element in the position it belongs by shunting other values backwards. For example, of there are 2 elements in the array correctly, start comparing the third/new element with the 2nd one (in position 1). If it is small, shunt the 2 nd one back and go on to compare with the first element. Shunting elements back will make room for the new one.

25 How much work is it to find the biggest element? In an array has n elements, then to find the biggest, we look at all of them: n amount of “work”

26 How much work to sort? We have to find the biggest of all n elements. Then we find the biggest of the remaining elements: n-1 comparisons. And so on. The last time, we just compare two. How many comparisons is this, for an array of size n?

27 Sorting names

28

29 Comparing names You shouldn’t use code like Aname<Othername since names are not an ordinal datatype even though some of my earlier examples did this. Instead, you should use the string method compareTo: Aname.compareTo(Othername) which returns an integer value.

30 Use string.compareTo(string) Returns 0 for equal Returns <0 for first less than second Returns >0 for first bigger than second An example: Suppose name is “Annie” and other is “Bob” name.compareTo(other) would return a value less than 0 since “Annie” comes before “Bob” in lexicographical ordering. Write code like Dim name as String=“Annie” Dim other as String=“Bob” If name.compareTo(other) then ‘code to handle less goes here Else ‘code to handle greater goes here endif

31 remember Make sure to count your entries – and only sort the entries, not the empty end of the array. Only display the entered portion of your array.


Download ppt "Arrays & Strings part 2 More sophisticated algorithms, including sorting."

Similar presentations


Ads by Google