Page 1
Data Structures: A Pseudocode Approach with C, Second Edition 1
Chapter 12
Objectives
Upon completion you will be able to:
• Understand the basic concepts of internal sorts
• Discuss the relative efficiency of different sorts
• Recognize and discuss selection, insertion and exchange sorts
Sorting
Page 2
Data Structures: A Pseudocode Approach with C, Second Edition 2
Page 3
Data Structures: A Pseudocode Approach with C, Second Edition 3
Page 4
Data Structures: A Pseudocode Approach with C, Second Edition 4
12-2 Selection Sorts
In each pass of the selection sort, the smallest element is
selected from the unsorted sublist and exchanged with the
element at the beginning of the unsorted list. We discuss two
classic selection sorts, straight selection and heap sort.
• Straight Selection Sort
• Heap Sort
• Selection Sort Efficiency
• Selection Sort Implementation
Page 5
Data Structures: A Pseudocode Approach with C, Second Edition 5
Page 6
Data Structures: A Pseudocode Approach with C, Second Edition 6
Page 7
Data Structures: A Pseudocode Approach with C, Second Edition 7
Page 8
Data Structures: A Pseudocode Approach with C, Second Edition 8
Page 9
Data Structures: A Pseudocode Approach with C, Second Edition 9
Page 10
Data Structures: A Pseudocode Approach with C, Second Edition 10
Page 11
Data Structures: A Pseudocode Approach with C, Second Edition 11
Page 12
Data Structures: A Pseudocode Approach with C, Second Edition 12
Page 13
Data Structures: A Pseudocode Approach with C, Second Edition 13
Page 14
Data Structures: A Pseudocode Approach with C, Second Edition 14
Page 15
Data Structures: A Pseudocode Approach with C, Second Edition 15
Page 16
Data Structures: A Pseudocode Approach with C, Second Edition 16
Page 17
Data Structures: A Pseudocode Approach with C, Second Edition 17
Page 18
Data Structures: A Pseudocode Approach with C, Second Edition 18
Page 19
Data Structures: A Pseudocode Approach with C, Second Edition 19
Page 20
Data Structures: A Pseudocode Approach with C, Second Edition 20
Page 21
Data Structures: A Pseudocode Approach with C, Second Edition 21
12-3 Insertion Sorts
In each pass of an insertion sort, one or more pieces of data are
inserted into their correct location in an ordered list. In this
section we study two insertion sorts: the straight insertion sort
and the shell sort.
• Straight Insertion Sort
• Shell Sort
• Insertion Sort Efficiency
• Insertion Sort Implementation
Page 22
Data Structures: A Pseudocode Approach with C, Second Edition 22
Page 23
Data Structures: A Pseudocode Approach with C, Second Edition 23
Page 24
Data Structures: A Pseudocode Approach with C, Second Edition 24
Page 25
Data Structures: A Pseudocode Approach with C, Second Edition 25
Page 26
Data Structures: A Pseudocode Approach with C, Second Edition 26
Page 27
Data Structures: A Pseudocode Approach with C, Second Edition 27
(continued)
Page 28
Data Structures: A Pseudocode Approach with C, Second Edition 28
Page 29
Data Structures: A Pseudocode Approach with C, Second Edition 29
Page 30
Data Structures: A Pseudocode Approach with C, Second Edition 30
Page 31
Data Structures: A Pseudocode Approach with C, Second Edition 31
Page 32
Data Structures: A Pseudocode Approach with C, Second Edition 32
Page 33
Data Structures: A Pseudocode Approach with C, Second Edition 33
Page 34
Data Structures: A Pseudocode Approach with C, Second Edition 34
Page 35
Data Structures: A Pseudocode Approach with C, Second Edition 35
12-4 Exchange Sorts
This section discusses the four basic queue operations. Using
diagrammatic figures, it shows how each of them work. It
concludes with a comprehensive example that demonstrates
each operation.
• Bubble Sort
• Quick Sort
• Exchange Sort Efficiency
• Sort Summary
• Exchange Sort Implementation
Page 36
Data Structures: A Pseudocode Approach with C, Second Edition 36
Page 37
Data Structures: A Pseudocode Approach with C, Second Edition 37
Page 38
Data Structures: A Pseudocode Approach with C, Second Edition 38
Page 39
Data Structures: A Pseudocode Approach with C, Second Edition 39
Page 40
Data Structures: A Pseudocode Approach with C, Second Edition 40
Page 41
Data Structures: A Pseudocode Approach with C, Second Edition 41
Page 42
Data Structures: A Pseudocode Approach with C, Second Edition 42
Page 43
Data Structures: A Pseudocode Approach with C, Second Edition 43
Page 44
Data Structures: A Pseudocode Approach with C, Second Edition 44
Page 45
Data Structures: A Pseudocode Approach with C, Second Edition 45
Page 46
Data Structures: A Pseudocode Approach with C, Second Edition 46
Page 47
Data Structures: A Pseudocode Approach with C, Second Edition 47
12-5 External Sorts
In external sorting portions of the data may be stored in
secondary memory during the sorting process. Included in this
section is a discussion of file merging and three external sort
approaches-natural, balanced, and polyphase.
• Merging Ordered Files
• Merging Unordered Files
• The Sorting Process
• Sort Phase Revisited
Page 48
Data Structures: A Pseudocode Approach with C, Second Edition 48
Page 49
Data Structures: A Pseudocode Approach with C, Second Edition 49
Page 50
Data Structures: A Pseudocode Approach with C, Second Edition 50
Page 51
Data Structures: A Pseudocode Approach with C, Second Edition 51
Page 52
Data Structures: A Pseudocode Approach with C, Second Edition 52
Page 53
Data Structures: A Pseudocode Approach with C, Second Edition 53
Page 54
Data Structures: A Pseudocode Approach with C, Second Edition 54
Page 55
Data Structures: A Pseudocode Approach with C, Second Edition 55
Page 56
Data Structures: A Pseudocode Approach with C, Second Edition 56
Page 57
Data Structures: A Pseudocode Approach with C, Second Edition 57