1 / 18

Chapter 7 – Arrays

Chapter 7 – Arrays. 7.1 Creating and Accessing Arrays 7.2 Using Arrays 7.3 Some Additional Types of Arrays 7.4 Sorting and Searching 7.5 Two-Dimensional Arrays. 7.4 Sorting and Searching. Bubble Sort Searching Linear Binary. Sorting. Sorting is an algorithm for ordering an array.

skah
Télécharger la présentation

Chapter 7 – Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using Arrays 7.3 Some Additional Types of Arrays 7.4 Sorting and Searching 7.5 Two-Dimensional Arrays

  2. 7.4 Sorting and Searching • Bubble Sort • Searching • Linear • Binary

  3. Sorting • Sorting is an algorithm for ordering an array. • Both use the swap algorithm: temp = varl varl = var2 var2 = temp

  4. Example 1 Alphabetize Two Words txtFirstWord txtSecondWord btnAlphabetize txtResult

  5. Swap Algorithm Private SubbtnAlphabetize_Click(...)HandlesbtnAlphabetize.Click DimfirstWord, secondWord, temp As String firstWord = txtFirstWord.Text secondWord = txtSecondWord.Text If(firstWord > secondWord) Then temp = firstWord firstWord = secondWord secondWord = temp End If txtResult.Text = firstWord & " before " & secondWord End Sub

  6. Bubble Sort Algorithm: n Items • Compare the first and second items. If they are out of order, swap them. • Compare the second and third items. If they are out of order, swap them. • Repeat this pattern for all remaining pairs. The final comparison and possible swap are between the next-to-last and last items.

  7. Bubble Sort Algorithm • The last item will be at its proper place. • Do another pass through first n – 1 items. • Repeat this process with one less item for each pass until a pass uses only the first and second items.

  8. Example 2 Sorting Flintstones • This program will sort an array of strings. The elements of the array will consist of the Flintstones’ names. Before After

  9. Bubble Sort Example Public Class frmFlintStones Dim person() As String = {"Pebbles", "Barney", "Wilma", "Fred", "Dino"} PrivateSub frmFlintStones_Load(..) Handles MyBase.Load 'Display unsorted list For i As Integer = 0 To 4 lstPeople.Items.Add(person(i)) Next End Sub

  10. Bubble Sort Algorithm Private Sub btnSort_Click(…) Handles btnSort.Click 'Bubble sort names Dim temp AsString ForpassNumAsInteger = 1 To 4 'Number of passes is 1 less than 'number of items ForiAsInteger = 0 To 5 - passNum - 1 'Each pass needs 1 less comparison If (person(i) > person(i + 1)) Then temp = person(i) person(i) = person(i + 1) person(i + 1) = temp EndIf Next Next

  11. Bubble Sort (Continued) 'Display alphabetized list ListBox1.Items.Add("") ListBox1.Items.Add("Sorted List") ForiAsInteger = 0 To 4 ListBox1.Items.Add(person(i)) Next End Sub

  12. Bubble Sort with Output for each Pass 'Bubble sort names Dim person() As String = {"Fred", "Pebbles", "Wilma", "Dino", "Barney"} Dim temp As String For passNum As Integer = 1 To 4 'Number of passes is 1 less than number of items For i As Integer = 0 To 4 - passNum 'Each pass needs 1 less comparison If (person(i) > person(i + 1)) Then temp = person(i) person(i) = person(i + 1) person(i + 1) = temp End If Next lstPeople.Items.Add("Pass Number: " & passNum) For j As Integer = 0 To 4 lstPeople.Items.Add(person(j)) Next lstPeople.Items.Add("") Next 'Display alphabetized list lstPeople.Items.Add("Sorted List") For i As Integer = 0 To 4 lstPeople.Items.Add(person(i)) Next You will need to generalize a bubble sort to work with n pieces of data (where n = the number of values in the array).

  13. Searching • Sequential search starts at the beginning of a list and keeps looking one by one until the item is found or the end of the list is reached. • For a sequential search, the list need not be sorted.

  14. Binary Search • Usually more efficient than sequential search • List must be sorted

  15. Binary Search: Algorithm • Given: an array in ascending order and a sought-after value, quarry, that may be in the array. • Repeatedly halve the range of indices where quarry might be found. • Halving routine looks at the middle value of the current range and compares it to quarry with =, >, and <. • If middle value = quarry, then search is over. • If middle value > quarry, then we can limit our search to the half of the range below the middle value. • If middle value < quarry, then we can limit our search to the half of the range above the middle value.

  16. Binary Search: Variables first – lower limit of range of values to search last – upper limit of range of values to search middle = Int((first + last) / 2) a( ) – ordered array to be searched foundFlag – True when quarry is found Note: If quarry is not in the array, eventually last will be greater than first. Note: Initially first = 0 and last = a.GetUpperBound(0)

  17. Binary Search: Code Do While (first <= last) And (Not FoundFlag) middle = CInt((first + last) / 2) Select Case quarry Case a(middle) foundFlag = True Case Is < a(middle) last = middle – 1 Case Is > a(middle) first = middle + 1 End Select Loop

  18. Binary Search: Notes • If a binary search ends with foundFlag = True, the subscript of the found item might be useful. • This would be the case if the array were an array of structures that was ordered with respect to one of its members. • The binary search would serve as an efficient table lookup process.

More Related