Programming Logic and Design, Fourth Edition, Comprehensive

advertisement
Programming Logic and
Design
Fourth Edition, Comprehensive
Chapter 9
Advanced Array Manipulation
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
Programming Logic and Design, Fourth Edition, Comprehensive
2
Objectives (continued)
• Use indexed files
• Use a linked list
• Use multidimensional arrays
Programming Logic and Design, Fourth Edition, Comprehensive
3
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
Programming Logic and Design, Fourth Edition, Comprehensive
4
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
Programming Logic and Design, Fourth Edition, Comprehensive
5
Understanding How to Swap Two
Values
• Swapping two values: reversing their position
• Use a temporary variable to hold one value
Programming Logic and Design, Fourth Edition, Comprehensive
6
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
Programming Logic and Design, Fourth Edition, Comprehensive
7
Using a Bubble Sort (continued)
• Developing the application:
– Mainline logic
Programming Logic and Design, Fourth Edition, Comprehensive
8
Using a Bubble Sort (continued)
• Developing the application:
– housekeeping() module
Programming Logic and Design, Fourth Edition, Comprehensive
9
Using a Bubble Sort (continued)
• Developing the application:
– switchValues() module
Programming Logic and Design, Fourth Edition, Comprehensive
10
Using a Bubble Sort (continued)
• Developing the application:
– When to call switchValues() module
Programming Logic and Design, Fourth Edition, Comprehensive
11
Using a Bubble Sort (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
12
Using a Bubble Sort (continued)
• Start with x = 0, compare first pair and swap
• With x = 1, compare next pair and swap
Programming Logic and Design, Fourth Edition, Comprehensive
13
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
Programming Logic and Design, Fourth Edition, Comprehensive
14
Using a Bubble Sort (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
15
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
Programming Logic and Design, Fourth Edition, Comprehensive
16
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
Programming Logic and Design, Fourth Edition, Comprehensive
17
Refining the Bubble Sort by Using a
Constant for Array Size (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
18
Refining the Bubble Sort by Using a
Constant for Array Size (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
19
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
Programming Logic and Design, Fourth Edition, Comprehensive
20
Sorting a List of Variable Size
(continued)
Programming Logic and Design, Fourth Edition, Comprehensive
21
Sorting a List of Variable Size (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
22
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
Programming Logic and Design, Fourth Edition, Comprehensive
23
Refining the Bubble Sort by Reducing
Unnecessary Comparisons (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
24
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
Programming Logic and Design, Fourth Edition, Comprehensive
25
Refining the Bubble Sort by Eliminating
Unnecessary Passes (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
26
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
Programming Logic and Design, Fourth Edition, Comprehensive
27
Using an Insertion Sort (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
28
Using an Insertion Sort (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
29
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
Programming Logic and Design, Fourth Edition, Comprehensive
30
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
Programming Logic and Design, Fourth Edition, Comprehensive
31
Using a Selection Sort (continued)
Programming Logic and Design, Fourth Edition, Comprehensive
32
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
Programming Logic and Design, Fourth Edition, Comprehensive
33
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
Programming Logic and Design, Fourth Edition, Comprehensive
34
Using Linked Lists
• Linked list: requires one extra field in every record to
hold the physical address of the next logical record
Programming Logic and Design, Fourth Edition, Comprehensive
35
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
Programming Logic and Design, Fourth Edition, Comprehensive
36
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
Programming Logic and Design, Fourth Edition, Comprehensive
37
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
Programming Logic and Design, Fourth Edition, Comprehensive
38
Using Multidimensional Arrays
(continued)
Programming Logic and Design, Fourth Edition, Comprehensive
39
Using Multidimensional Arrays
(continued)
Programming Logic and Design, Fourth Edition, Comprehensive
40
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-ofplace element to its proper place within the part of the
array already processed
Programming Logic and Design, Fourth Edition, Comprehensive
41
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
Programming Logic and Design, Fourth Edition, Comprehensive
42
Download