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

Arrays & Strings part 2 More sophisticated algorithms, including sorting

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

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

Arrays & Strings part 2

More sophisticated algorithms, including sorting

Page 2: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Applications in this show

• Palindrome

• Sorting values

• Sort names

Page 3: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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.

Page 4: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Using combo box and stringsPrivate 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

Page 5: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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.

Page 6: Arrays & Strings part 2 More sophisticated algorithms, including sorting

An exercise: Shift array contents to next (higher) index

• Move array contents one location farther, so 0th value goes in 1st place, 1st place goes into 2nd 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

Page 7: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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

Page 8: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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 0th spot values(0) =save

What does the array look like now?

Page 9: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Rotate right

• Exercise.

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

Page 10: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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

Page 11: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Palindrome: spells the same forwards or backwards

Page 12: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Palindrome

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

Page 13: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Palindrome

Page 14: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Palindrome

• In button click, go through the string to make sure first char matches the last

• Next char matches next-to-last, etc.

• Display result

Page 15: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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.

Page 16: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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 2nd largest, third, and positioning them correctly.

Page 17: Arrays & Strings part 2 More sophisticated algorithms, including sorting

build this interfaceinclude try catch for format and indexoutofbounds

Page 18: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Sorting from big to small

Page 19: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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

Page 20: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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

Page 21: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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

Page 22: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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.

Page 23: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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?

Page 24: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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 2nd one back and go on to compare with the first element.

• Shunting elements back will make room for the new one.

Page 25: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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”

Page 26: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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?

Page 27: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Sorting names

Page 28: Arrays & Strings part 2 More sophisticated algorithms, including sorting

Sorting names

Page 29: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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.

Page 30: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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 hereElse‘code to handle greater goes hereendif

Page 31: Arrays & Strings part 2 More sophisticated algorithms, including sorting

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.