Sorting - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Sorting

Description:

Sorting is a process through which data are arranged according to their values. ... Knuth suggests that when the sort partition becomes small a straight insertion ... – PowerPoint PPT presentation

Number of Views:211
Avg rating:3.0/5.0
Slides: 57
Provided by: ValuedGate941
Category:
Tags: knuth | sorting

less

Transcript and Presenter's Notes

Title: Sorting


1
Chapter 12 Sorting
2
Sort Concepts
  • Sorting is a process through which data are
    arranged according to their values.
  • It is the most common data-processing
    applications.

3
Sort Classifications
  • Sorting algorithms are classified as either
    internal or external.
  • Internal sort holds all data 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 not fit in primary memory.

4
Sort Classifications
5
Sort Order
  • The sort order identifies the sequence of sorted
    data, ascending or descending.

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

7
Sort Stability
8
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.
  • Two classic selection sorts
  • Straight selection
  • Heap sort.

9
Straight Selection Sort
  • In the straight selection sort, the list at any
    moment is divided into two sublists, sorted and
    unsorted, which are divided by an imaginary wall.
  • 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.

10
Straight Selection Sort
11
Straight Selection Sort
12
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.

13
Heap Representations
FIGURE 12-5 Heap Representations
14
Heap Sort Exchange Process
15
Heap Sort Process
16
Comparison of Selection Sorts
17
Straight Insertion Sort
  • In a 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.

18
Straight Insertion Sort
19
Straight Insertion Sort
20
Shell Sort
  • The shell sort is an improved version of the
    straight insertion sort in which diminishing
    partitions are used to sorted the data.

21
Shell Sort
N 10 Increment (K) 3
22
Shell Sort
23
Shell Sort
24
Shell Sort
25
Comparison of Sorts
26
Exchanged Sort
  • In exchange sorts we exchange elements that are
    out of order until the entire list is sorted.
  • Bubble Sort
  • Quick Sort

27
Bubble Sort
  • In the bubble sort, 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.

28
Bubble Sort
FIGURE 12-12 Bubble Sort Concept
29
Bubble Sort
FIGURE 12-13 Bubble Sort Example
30
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.

31
Quick Sort
  • Selects an element, known as pivot.
  • Divides the list into three groups
  • A partition of elements ? the pivots key
  • The pivot element that is placed in its
    ultimately correct location in the list.
  • A partition of elements ? the pivots key

32
Quick Sort
  • The sorting then continues by quick sorting the
    left partition followed by quick sorting the
    right partition.

33
Quick Sort
34
How to Select a Pivot
  • 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

35
Quick Sort 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.

36
Quick Sort
37
Quick Sort
38
Comparison of Sorts
TABLE 12-3 Sort Comparisons
39
External Sorting
  • In external sorting, portions of the data may be
    stored in secondary memory during the sorting
    process.

40
Merging Ordered Files
  • A merge is the process that combines two files
    sorted on a given key into one sorted file on the
    same given key.

41
Merging Ordered Files
42
Merging Unordered Files
  • In a merge sort, we usually have a different
    situation.
  • Because the files are unsorted, the data runs in
    sequence, and then there is a sequence break
    followed by another series of data in sequence.

43
Merging Unordered Files
  • The series of consecutively ordered data in a
    file is known as a merge run.
  • A stepdown occurs when the sequential ordering of
    the records in a merge file is broken.

44
Merging Unordered Files
  • The process of copying a consecutive series of
    records to the merge output file after a stepdown
    in the alternate merge input is known as a
    rollout.

45
Merging Unordered Files
46
Sorting Process
  • The first processing of the data into merge runs
    is known as sort phase.
  • Each complete reading and merging of the input
    merge files to one or more output merge files is
    considered a separate merge phase.

47
Sorting Process
48
Natural Merge
  • A natural merge sorts a constant number of input
    merge files to one merge output file.
  • Between each merge phase, a distribution phase is
    required to redistribute the merge runs to the
    input files for remerging.

49
Natural Merge
50
Balanced Merge
  • A balanced merge uses a constant number of input
    merge files and the same number of output merge
    files.
  • The balanced merge eliminates the distribution
    phase by using the same number of input and
    output merge files.

51
Balanced Merge
52
Polyphase Merge
  • In the polyphase merge, a constant number of
    input files are merged to one output file.
  • As the data in each input file are completely
    merged, it immediately becomes the output file
    and what was the output file becomes an input
    file.

53
Polyphase Merge Sort
FIGURE 12-22 Polyphase Merge Sort
54
External Sort Phase (Heap Sort)
  • Use a heap of three nodes to sort the data into
    two merge runs.
  • Fill the first merge run with non-decreasing
    number until it is full.
  • When the first merge run is full, fill the second
    merge run with non-decreasing number.

55
(No Transcript)
56
Exercises
Write a Comment
User Comments (0)
About PowerShow.com