Programming Logic and Design Fourth Edition, Comprehensive - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Programming Logic and Design Fourth Edition, Comprehensive

Description:

Programming Logic and Design Fourth Edition, Comprehensive – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 43
Provided by: Cerr4
Category:

less

Transcript and Presenter's Notes

Title: Programming Logic and Design Fourth Edition, Comprehensive


1
Programming Logic and DesignFourth Edition,
Comprehensive
  • Chapter 9
  • Advanced Array Manipulation

2
Objectives
  • Describe the need for sorting data
  • Swap two values in computer memory
  • Use a bubble sort
  • Use an insertion sort
  • Use a selection sort

3
Objectives (continued)
  • Use indexed files
  • Use a linked list
  • Use multidimensional arrays

4
Understanding the Need for Sorting Records
  • Sequential order records are arranged based on
    the value in a field (e.g., SSN, employee ID)
  • Random order records are in the order in which
    they were added
  • Sorting placing the records in order, based on
    the values in one or more fields
  • Ascending order arranged from lowest to highest
  • Descending order arranged from highest to lowest

5
Understanding the Need for Sorting Records
(continued)
  • Median value the middle item when values are
    listed in order
  • Mean value arithmetic average
  • Computer always sorts based on numeric values
  • Character data is sorted by its numeric code
    value
  • A is less than B
  • Whether A is greater than a is
    system-dependent

6
Understanding How to Swap Two Values
  • Swapping two values reversing their position
  • Use a temporary variable to hold one value

7
Using a Bubble Sort
  • Bubble sort
  • Items in a list are compared in pairs
  • If an item is out of order, it swaps places with
    the item below it
  • In an ascending sort, after a complete pass
    through the list, largest item has sunk to the
    bottom and smallest item has bubbled to the top

8
Using a Bubble Sort (continued)
  • Developing the application
  • Mainline logic

9
Using a Bubble Sort (continued)
  • Developing the application
  • housekeeping() module

10
Using a Bubble Sort (continued)
  • Developing the application
  • switchValues() module

11
Using a Bubble Sort (continued)
  • Developing the application
  • When to call switchValues() module

12
Using a Bubble Sort (continued)
13
Using a Bubble Sort (continued)
  • Start with x 0, compare first pair and swap
  • With x 1, compare next pair and swap

14
Using a Bubble Sort (continued)
  • With x 2, compare next pair, but no swap needed
  • With x 3, compare last pair, and swap
  • Largest value has sunk to the bottom

15
Using a Bubble Sort (continued)
16
Using a Bubble Sort (continued)
  • Use nested loops for sorting an array
  • Inner loop makes the pair comparisons
  • Greatest number of comparisons is one less than
    the number of array elements
  • Outer loop controls the number of times to
    process the list
  • One less than the number of array elements

17
Refining the Bubble Sort by Using a Constant for
the Array Size
  • Store the number of array elements in a constant
  • Makes code more readable
  • If number of elements changes later, only one
    change to the program is needed

18
Refining the Bubble Sort by Using a Constant for
Array Size (continued)
19
Refining the Bubble Sort by Using a Constant for
Array Size (continued)
20
Sorting a List of Variable Size
  • Use a variable to hold the number of array
    elements
  • Declare the array with a large fixed size
  • Count the number of elements when the file is
    read into the array
  • Use the count to control the loops when sorting,
    ignoring unused slots in the array

21
Sorting a List of Variable Size (continued)
22
Sorting a List of Variable Size (continued)
23
Refining the Bubble Sort by Reducing Unnecessary
Comparisons
  • After the first pass through the array
  • Largest item must be at the end of array
  • Second largest item must be at the second-to-last
    position in the array
  • Not necessary to compare those two values again
  • On each subsequent pass through the array, stop
    the pair comparisons one element sooner

24
Refining the Bubble Sort by Reducing Unnecessary
Comparisons (continued)
25
Refining the Bubble Sort by Eliminating
Unnecessary Passes
  • Need one fewer pass than the number of elements
    to completely sort the array
  • If array is somewhat ordered already, can reduce
    the number of passes
  • Use a flag variable to indicate if there were any
    swaps during a single pass
  • If no swaps, the array is completely sorted

26
Refining the Bubble Sort by Eliminating
Unnecessary Passes (continued)
27
Using an Insertion Sort
  • Bubble sort is one of the least efficient sorting
    methods
  • Insertion sort usually requires fewer comparisons
  • Technique
  • Compare a pair of elements
  • If an element is smaller than the previous one,
    search the array backward from that point and
    insert this element at the proper location

28
Using an Insertion Sort (continued)
29
Using an Insertion Sort (continued)
30
Using a Selection Sort
  • Selection sort uses two variables to store the
    smallest value and its position in the array
  • Technique
  • Store first element value and its position in
    variables
  • Compare value of first element to value of next
    element
  • If next element is smaller, put its value and
    position in the variables
  • Continue until end of array, at which time the
    smallest value and its position are in the
    variables
  • Swap the first element value and position with
    the element and position stored in the variables

31
Using a Selection Sort (continued)
  • Technique (continued)
  • Start at the second element in the array and
    repeat the process
  • Continue until all elements except the last have
    been designated as the starting point
  • After making one less pass than the number of
    elements, the array is sorted

32
Using a Selection Sort (continued)
33
Using Indexed Files
  • When a large data file with thousands of records
    needs to be accessed in sorted order, usually
    only one field determines the sorting
  • Key field the field whose contents make the
    record unique
  • Indexing records create a list of key fields
    paired with their corresponding positions in the
    file
  • Sorting the indexes is faster than physically
    sorting the actual records
  • Random-access storage device disk or other
    device from which records can be accessed in any
    order

34
Using Indexed Files (continued)
  • Address location within computer memory or
    storage
  • Every data record on disk has an address
  • Index holds physical addresses and key field
    values
  • Data file is in physical order, but index is
    sorted in logical order
  • When a record is removed from an indexed file,
    does not have to be physically removed, just
    deleted from the index file

35
Using Linked Lists
  • Linked list requires one extra field in every
    record to hold the physical address of the next
    logical record

36
Using Linked Lists (continued)
  • To add a new record
  • Search the linked list for the correct logical
    location
  • Break the current link, and insert the new record
    by linking the previous record to the new record
    and linking the new record to the next record

37
Using Linked Lists (continued)
  • Two-way linked list
  • Two fields are added to the data record
  • One holds address of previous logical record
  • One holds address of next logical record
  • List can be accessed in either a forward or
    backward direction

38
Using Multidimensional Arrays
  • Single-dimensional (or one-dimensional) array
    represents a single list of values
  • Multidimensional array
  • A list with two or more related values in each
    position
  • Two-dimensional array
  • Represents values in a table or grid containing
    rows and columns
  • Requires two subscripts

39
Using Multidimensional Arrays (continued)
40
Using Multidimensional Arrays (continued)
41
Summary
  • Sort data records in ascending or descending
    order based on the contents of one or more fields
  • Swap two values by creating a temporary variable
    to hold one of the values
  • Bubble sort compares pairs and swaps pairs to
    obtain the desired order
  • Eliminate unnecessary comparisons in each pass
    and eliminate unnecessary passes to improve
    bubble sort
  • Insertion sort compares pairs, and moves the
    out-of-place element to its proper place within
    the part of the array already processed

42
Summary (continued)
  • Ascending selection sort uses two variables to
    hold the smallest value and its position in the
    array
  • Index can be used to access records in logical
    order without physically sorting the data file
  • Linked lists use an extra field to hold the
    address of the next record in the logical order
Write a Comment
User Comments (0)
About PowerShow.com