chapter12r.ppt lecture-10 (1).ppt lecture-10 (1).ppt

yatakonakiran2 17 views 89 slides Aug 31, 2024
Slide 1
Slide 1 of 89
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89

About This Presentation

lecture-10 (1).ppt


Slide Content

Data Structures: A Pseudocode Approach with C, Second Edition 1
Chapter 12Chapter 12
Objectives
Upon completion you will be able to:
• Understand the basic concepts of internal and external sorts
• Discuss the relative efficiency of different sorts
• Recognize and discuss selection, insertion and exchange sorts
• Discuss the design and operation of external sorts
• Recognize natural, balanced, and polyphase merge sorts
SortingSorting

Data Structures: A Pseudocode Approach with C, Second Edition 2
12-1 Sort Concepts
Sorts arrange data according to their value. In this chapter, we Sorts arrange data according to their value. In this chapter, we
discuss both internal and external sorts. We divide internal sorts discuss both internal and external sorts. We divide internal sorts
into three categories-insertion, selection, and exchange-and into three categories-insertion, selection, and exchange-and
discuss two different sorts in each classification. At the end of discuss two different sorts in each classification. At the end of
the chapter we discuss natural, balanced, and polyphase external the chapter we discuss natural, balanced, and polyphase external
sorts. We begin with a discussion of several sort concepts in sorts. We begin with a discussion of several sort concepts in
Section 12.1.Section 12.1.
• Sort Order
• Sort Stability
• Sort Efficiency
• Passes
• Sorts and ADTs

Data Structures: A Pseudocode Approach with C, Second Edition 3
Sort concepts

Sort

The process through which data are
arranged according to their values.

Sorting is one of the most common
data-processing applications.

Data Structures: A Pseudocode Approach with C, Second Edition 4
Sort classifications

Data Structures: A Pseudocode Approach with C, Second Edition 5
Sort order

The sort order identifies the sequence
of sorted data, ascending or
descending.

Data Structures: A Pseudocode Approach with C, Second Edition 6
Sort order

Data Structures: A Pseudocode Approach with C, Second Edition 7
Sort stability

Sort stability is an attribute of a sort,
indicating that data with equal keys
maintain their relative input order in the
output.

Data Structures: A Pseudocode Approach with C, Second Edition 8
Sort efficiency

Sort efficiency is a
measure of the
relative efficiency of
a sort.

Quick sort (best)

)log(nnO

Data Structures: A Pseudocode Approach with C, Second Edition 9
Passes

During the sorting process, the data
traversed many times.

Each traversal of the data is referred as
a sort pass.

Data Structures: A Pseudocode Approach with C, Second Edition 10
12-2 Selection Sorts
In each pass of the selection sort, the smallest element is selected In each pass of the selection sort, the smallest element is selected
from the unsorted sublist and exchanged with the element at the from the unsorted sublist and exchanged with the element at the
beginning of the unsorted list. We discuss two classic selection beginning of the unsorted list. We discuss two classic selection
sorts, straight selection and heap sort.sorts, straight selection and heap sort.
• Straight Selection Sort
• Heap Sort
• Selection Sort Efficiency
• Selection Sort Implementation

Data Structures: A Pseudocode Approach with C, Second Edition 11
Selection sorts

Selection sorts are among the most intuitive
of all sorts.

Straight Selection Sort

Heap sort

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.

Data Structures: A Pseudocode Approach with C, Second Edition 12
Straight selection sort

Data Structures: A Pseudocode Approach with C, Second Edition 13
Selection sort

Data Structures: A Pseudocode Approach with C, Second Edition 14
Selection sort algorithm

Data Structures: A Pseudocode Approach with C, Second Edition 15
Selection sort algorithm

Data Structures: A Pseudocode Approach with C, Second Edition 16
Heap sort

The heap sort is an improved version of
the selection sort in which the largest
element (the root) is selected and
exchanged with the last element in the
unsorted list.

Data Structures: A Pseudocode Approach with C, Second Edition 17

Data Structures: A Pseudocode Approach with C, Second Edition 18
Heap sort exchange process

Data Structures: A Pseudocode Approach with C, Second Edition 19
Heap sort process

Data Structures: A Pseudocode Approach with C, Second Edition 20

Data Structures: A Pseudocode Approach with C, Second Edition 21
Selection sort efficiency

The straight selection sort efficiency is
O(n
2
).

The heap sort efficiency is O(n log n)

Data Structures: A Pseudocode Approach with C, Second Edition 22
Selection sort
implementatio
n

Data Structures: A Pseudocode Approach with C, Second Edition 23

Data Structures: A Pseudocode Approach with C, Second Edition 24
Heap sort
implementatio
n

Data Structures: A Pseudocode Approach with C, Second Edition 25

Data Structures: A Pseudocode Approach with C, Second Edition 26

Data Structures: A Pseudocode Approach with C, Second Edition 27

Data Structures: A Pseudocode Approach with C, Second Edition 28

Data Structures: A Pseudocode Approach with C, Second Edition 29

Data Structures: A Pseudocode Approach with C, Second Edition 30
12-3 Insertion Sorts
In each pass of an insertion sort, one or more pieces of data are In each pass of an insertion sort, one or more pieces of data are
inserted into their correct location in an ordered list. inserted into their correct location in an ordered list.
In this section we study two insertion sorts: the straight insertion In this section we study two insertion sorts: the straight insertion
sort and the shell sort.sort and the shell sort.
• Straight Insertion Sort
• Shell Sort
• Insertion Sort Efficiency
• Insertion Sort Implementation

Data Structures: A Pseudocode Approach with C, Second Edition 31
Straight insertion sort

The list at any moment is divided into
sorted and unsorted sublists.

In each pass the first element of the
unsorted sublist is inserted into the
sorted sublist.

Data Structures: A Pseudocode Approach with C, Second Edition 32
Straight insertion sort

Data Structures: A Pseudocode Approach with C, Second Edition 33
Straight
insertion sort
example

Data Structures: A Pseudocode Approach with C, Second Edition 34

Data Structures: A Pseudocode Approach with C, Second Edition 35
Shell sort

The shell sort is an improved version of
the straight insertion sort in which
diminishing partitions are used to
sorted the data.

Data Structures: A Pseudocode Approach with C, Second Edition 36
Segmented array

N = 10

Increment (K) = 3

Data Structures: A Pseudocode Approach with C, Second Edition 37
Diminishing increments in shell
sort

Data Structures: A Pseudocode Approach with C, Second Edition 38
Diminishing increments in shell
sort

Data Structures: A Pseudocode Approach with C, Second Edition 39
Diminishing increments in shell
sort

Data Structures: A Pseudocode Approach with C, Second Edition 40
Diminishing increments in shell
sort

Data Structures: A Pseudocode Approach with C, Second Edition 41

Data Structures: A Pseudocode Approach with C, Second Edition 42

Data Structures: A Pseudocode Approach with C, Second Edition 43
Straight insertion sort
efficiency

O(N
2
)

Data Structures: A Pseudocode Approach with C, Second Edition 44
Shell sort efficiency

O(N
1.25
)

Data Structures: A Pseudocode Approach with C, Second Edition 45
Comparison of insertion and
selection sorts

Data Structures: A Pseudocode Approach with C, Second Edition 46

Data Structures: A Pseudocode Approach with C, Second Edition 47

Data Structures: A Pseudocode Approach with C, Second Edition 48

Data Structures: A Pseudocode Approach with C, Second Edition 49
12-4 Exchange Sorts
• Bubble Sort
• Quick Sort
• Exchange Sort Efficiency
• Sort Summary
• Exchange Sort Implementation

Data Structures: A Pseudocode Approach with C, Second Edition 50
Exchanged sort

In exchange sorts we exchange
elements that are out of order until the
entire list is sorted.

Data Structures: A Pseudocode Approach with C, Second Edition 51
Bubble sort

Data Structures: A Pseudocode Approach with C, Second Edition 52
Bubble sort concept

Data Structures: A Pseudocode Approach with C, Second Edition 53
Bubble sort
example

Data Structures: A Pseudocode Approach with C, Second Edition 54

Data Structures: A Pseudocode Approach with C, Second Edition 55
Quick sort

Quick sort is an exchange sort in which
a pivot key is placed in its correct
position in the array while rearranging
other elements widely dispersed across
the list.

Data Structures: A Pseudocode Approach with C, Second Edition 56
Quick sort
Each iteration of the quick sort

selects an element, known as pivot, and

Divides the list into three groups

A partition of elements < the pivot’s key

The pivot element that is placed in its ultimately
correct location in the list

A partition of elements the pivot’s key

The sorting then continues by quick
sorting the left partition followed by
quick sorting the right partition.

Data Structures: A Pseudocode Approach with C, Second Edition 57
Quick sort partitions

Data Structures: A Pseudocode Approach with C, Second Edition 58
Quick sort algorithm

Straight insertion module

Determine median of three

Quick sort algorithm

Data Structures: A Pseudocode Approach with C, Second Edition 59

Data Structures: A Pseudocode Approach with C, Second Edition 60

Data Structures: A Pseudocode Approach with C, Second Edition 61
Quick
sort
pivot

Data Structures: A Pseudocode Approach with C, Second Edition 62
Quick sort operation

Data Structures: A Pseudocode Approach with C, Second Edition 63

Data Structures: A Pseudocode Approach with C, Second Edition 64

Data Structures: A Pseudocode Approach with C, Second Edition 65
Exchange sort efficiency

The bubble sort efficiency is O(n
2
).

The quick sort efficiency is O(n log n).

Data Structures: A Pseudocode Approach with C, Second Edition 66
Sort comparisons

Data Structures: A Pseudocode Approach with C, Second Edition 67

Data Structures: A Pseudocode Approach with C, Second Edition 68

Data Structures: A Pseudocode Approach with C, Second Edition 69

Data Structures: A Pseudocode Approach with C, Second Edition 70

Data Structures: A Pseudocode Approach with C, Second Edition 71

Data Structures: A Pseudocode Approach with C, Second Edition 72

Data Structures: A Pseudocode Approach with C, Second Edition 73

Data Structures: A Pseudocode Approach with C, Second Edition 74

Data Structures: A Pseudocode Approach with C, Second Edition 75

Data Structures: A Pseudocode Approach with C, Second Edition 76
12-5 External Sorts
In external sorting portions of the data may be stored in In external sorting portions of the data may be stored in
secondary memory during the sorting process. Included in this secondary memory during the sorting process. Included in this
section is a discussion of file merging and three external sort section is a discussion of file merging and three external sort
approaches-natural, balanced, and polyphase.approaches-natural, balanced, and polyphase.
• Merging Ordered Files
• Merging Unordered Files
• The Sorting Process
• Sort Phase Revisited

Data Structures: A Pseudocode Approach with C, Second Edition 77

Data Structures: A Pseudocode Approach with C, Second Edition 78

Data Structures: A Pseudocode Approach with C, Second Edition 79

Data Structures: A Pseudocode Approach with C, Second Edition 80

Data Structures: A Pseudocode Approach with C, Second Edition 81

Data Structures: A Pseudocode Approach with C, Second Edition 82

Data Structures: A Pseudocode Approach with C, Second Edition 83

Data Structures: A Pseudocode Approach with C, Second Edition 84

Data Structures: A Pseudocode Approach with C, Second Edition 85
12-6 Quick Sort Efficiency
We revisit the general approach to analyzing algorithm We revisit the general approach to analyzing algorithm
efficiency with an in-depth discussion of the quick sort's efficiency with an in-depth discussion of the quick sort's
efficiency.efficiency.

Data Structures: A Pseudocode Approach with C, Second Edition 86

Data Structures: A Pseudocode Approach with C, Second Edition 87

Data Structures: A Pseudocode Approach with C, Second Edition 88

Data Structures: A Pseudocode Approach with C, Second Edition 89