Chapter 7 Arrays - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Chapter 7 Arrays

Description:

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 ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 19
Provided by: cwy7
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 Arrays


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
  • Shell Sort (Not on the test)
  • Searching

3
Sorting
  • Sorting is an algorithm for ordering an array.
  • We will discuss two sorting algorithms
  • Bubble sort
  • Shell sort
  • Both use the swap algorithm
  • temp varl
  • varl var2
  • var2 temp

4
Example 1 Alphabetize Two Words
txtFirstWord
txtSecondWord
btnAlphabetize
txtResult
5
Example 1 Swap Algorithm
  • Private Sub btnAlphabetize_Click(...)Handles
    btnAlphabetize.Click
  • Dim firstWord, secondWord, temp As String
  • firstWord txtFirstWord.Text
  • secondWord txtSecondWord.Text
  • If (firstWord gt 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
  • Public Class frmFlintStones
  • Dim person() As String "Pebbles", "Barney",
    "Wilma", "Fred", "Dino"
  • Private Sub 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 (Continued)
  • Private Sub btnSort_Click(ByVal sender As
    System.Object, ByVal e As System.EventArgs)
    Handles btnSort.Click
  • 'Bubble sort names
  • 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 1 To 5 - passNum
  • 'Each pass needs 1 less comparison
  • If (person(i - 1) gt person(i)) Then
  • temp person(i - 1)
  • person(i - 1) person(i)
  • person(i) temp
  • End If
  • Next
  • Next

11
Bubble Sort (Continued)
  • 'Display alphabetized list
  • lstPeople.Items.Clear()
  • For i As Integer 0 To 4
  • lstPeople.Items.Add(person(i))
  • Next
  • End Sub

12
Shell Sort Algorithm
  • Begin with a gap of g Int(n/2)
  • Compare items 0 and g, 1 and 1 g, . . ., n - g
    and n. Swap any pairs that are out of order.
  • Repeat Step 2 until no swaps are made for gap g.
  • Halve the value of g.
  • Repeat Steps 2, 3, and 4 until the value of g is
    0.

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 ,
    gt, and lt.
  • If middle value quarry, then search is over.
  • If middle value gt quarry, then we can limit our
    search to the half of the range below the middle
    value.
  • If middle value lt 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 lt last) And (Not FoundFlag)
  • middle CInt((first last) / 2)
  • Select Case a(middle)
  • Case quarry
  • foundFlag True
  • Case Is gt quarry
  • last middle 1
  • Case Is lt quarry
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com