Objectives - PowerPoint PPT Presentation

1 / 90
About This Presentation
Title:

Objectives

Description:

We discuss two classic selection sorts, straight selection and heap sort. ... Invalid heaps. Data Structures: A Pseudocode Approach with C, Second Edition. 19 ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 91
Provided by: ValuedGate719
Category:
Tags: heaps | objectives

less

Transcript and Presenter's Notes

Title: Objectives


1
Chapter 12
Sorting(1)
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

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

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.

4
Sort classifications
  • Sorting algorithms are classified as either
    internal or external.
  • Internal sort
  • All data are hold in primary memory during the
    sorting process.
  • External sort
  • Uses primary memory for the data currently being
    sorted and secondary storage for any data that
    does fit in primary memory.

5
Sort classifications
6
Sort order
  • The sort order identifies the sequence of sorted
    data, ascending or descending.

7
Sort Staility
  • The sort order identifies the sequence of sorted
    data, ascending or descending.

8
Sort efficiency
  • Sort efficiency is a measure of the relative
    efficiency of a sort.
  • Quick sort (best)
  • O(n logn)

9
Passes
  • During the sorting process, the data traversed
    many times.
  • Each traversal of the data is referred as a sort
    pass.

10
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

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.

12
Straight selection sort
13
Selection sort
14
Selection sort algorithm
15
9-1 Basic Concepts
  • A heap is a binary tree whose left and right
    subtrees have values less than their parents. We
    begin with a discussion of the basic heap
    structure and its two primary operations, reheap
    up and reheap down.
  • Definition
  • Maintenance Operations

16
Definition
  • A heap is a binary tree structure with the
    following properties
  • The tree is complete or nearly complete.
  • The key value of each node is greater than or
    equal to the key value in each of its descendents.

17
Heap trees
18
Invalid heaps
19
Maintenance operations
  • Reheap up
  • Reorders a broken heap by floating the last
    element up the tree until it is in its correct
    location in the heap.
  • Reheap down
  • Reorders a broken heap by pushing the last
    element up the tree until it is in its correct
    location in the heap.

20
Reheap up operation
21
Reheap up example
22
Reheap down operation
23
Reheap down example
24
9-2 Heap Implementation
  • Heaps are usually implemented in an array
    structure. In this section we discuss and develop
    five heap algorithms.
  • Reheap Up
  • Reheap Down
  • Build a Heap
  • Insert a Node into a Heap
  • Delete a Node from a Heap

25
Heap in arrays
26
Building a heap
27
Insert node
28
Insert node
29
Delete node
30
Delete node
31
(No Transcript)
32
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.

33
Heap representations
23, 78, 45, 8, 32, 56.
78
32
56
8
23
45
0
1
2
3
4
5
34
Heap sort exchange process
35
Heap sort process
36
(No Transcript)
37
Selection sort efficiency
  • The straight selection sort efficiency is O(n2).
  • The heap sort efficiency is O(n log n)

38
Selection sort implementation
39
(No Transcript)
40
Heap sort implementation
41
(No Transcript)
42
(No Transcript)
43
(No Transcript)
44
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

45
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.

46
Straight insertion sort
47
Straight insertion sort example
48
(No Transcript)
49
Shell sort
  • The shell sort is an improved version of the
    straight insertion sort in which diminishing
    partitions are used to sorted the data.

50
Segmented array
  • N 10
  • Increment (K) 3

51
Diminishing increments in shell sort
52
Diminishing increments in shell sort
53
Diminishing increments in shell sort
54
Diminishing increments in shell sort
55
incre current hold walker hold key walker key
56
Ordered segment in a Shell sort
57
Straight insertion sort efficiency
  • O(N2)

58
Shell sort efficiency
  • O(N1.25)

59
Comparison of insertion and selection sorts
60
(No Transcript)
61
(No Transcript)
62
(No Transcript)
63
12-4 Exchange Sorts
  • Bubble Sort
  • Quick Sort
  • Exchange Sort Efficiency
  • Sort Summary
  • Exchange Sort Implementation

64
Exchanged sort
  • In exchange sorts we exchange elements that are
    out of order until the entire list is sorted.
  • Bubble sort
  • Quick sort

65
Bubble sort concept
  • The list in any moment is divided into two
    sublists sorted and unsorted.
  • The smallest element is bubbled from the unsorted
    sublist and moved to the sorted sublist.

66
Bubble sort example
67
(No Transcript)
68
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.

69
Quick sort
  • Each iteration of the quick sort
  • selects an element, known as pivot, and
  • Divides the list into three groups
  • 1. A partition of elements lt the pivots key
  • 2. The pivot element that is placed in its
    ultimately correct location in the list
  • 3. A partition of elements ? the pivots key
  • The sorting then continues by quick sorting the
    left partition followed by quick sorting the
    right partition.

70
Quick sort partitions
71
How to select a prvot
  • C. A. R. Hoare (1962)
  • the first element in the list
  • R. C. Singleton (1969)
  • the median value of left, right and middle
    elements in the list

72
Note
  • Knuth suggests that when the sort partition
    becomes small a straight insertion sort be used
    to complete the sorting of the partition.
  • The authors of our textbook recommend 16.

73
Quick sort algorithm
  • Quick sort algorithm
  • Straight insertion module (supporting)
  • Determine median of three (supporting)

74
(No Transcript)
75
??
??
??
76
Quick sort pivot
77
Quick sort operation
78
(No Transcript)
79
(No Transcript)
80
Exchange sort efficiency
  • The bubble sort efficiency is O(n2).
  • The quick sort efficiency is O(n log n).

81
Sort comparisons
82
(No Transcript)
83
(No Transcript)
84
(No Transcript)
85
(No Transcript)
86
(No Transcript)
87
(No Transcript)
88
(No Transcript)
89
(No Transcript)
90
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com