Advanced Sorting Methods: Shellsort - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced Sorting Methods: Shellsort

Description:

Algorithm merge (source, destination, lower, mid, upper) ... partitioned form, where pivotPoint is an index of the final destination of the. pivot ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 19
Provided by: csC5
Learn more at: http://www.cs.ccsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Advanced Sorting Methods: Shellsort


1
Advanced Sorting Methods Shellsort
  • Shellsort is an extension of insertion sort,
    which gains speed by allowing
  • exchanges of elements that are far apart.
  • The idea Rearrange the file to give it a
    property that taking very h-th element
  • (starting anywhere) yields a sorted file, called
    h-sorted. That is, h-sorted file
  • is "h" independent sorted files interleaved
    together.
  • Example Let h 13 during the first step, h
    4 during the second step, and during the final
    step h 1 (insertion sort at this step)
  • Step1 15 8 7 3 2 14 11
    1 5 9 4 12 13 6 10

  • compare and exchange
  • Step2 6 8 7 3 2 14 11
    1 5 9 4 12 13 15 10
  • Step 3 2 8 4 1 5 9 7
    3 6 14 10 12 13 15 11

2
  • To implement Shell sort we need a helper method,
    SegmentedInsertionSort.
  • Input A, input array
  • N, number of elements
  • H, distance between elements in the
    same segment.
  • Output Array, A, H-sorted.
  • Algorithm SegmentedInsertionSort (A, N, H)
  • for l H 1 to N do
  • j l H / j counts down through the
    current segment /
  • while j gt 0 do
  • if precedes (Aj H, Aj) then
  • swap (Aj H, Aj)
  • j j H
  • else
  • j 0
  • endif

3
  • The Shell sort method now becomes
  • Input A, input array
  • N number of elements
  • Output Array, A, sorted.
  • Algorithm ShellSort (A, N)
  • H N / 2
  • while H gt 0 do
  • SegmentedInsertionSort (A, N, H)
  • H H / 2
  • endwhile
  • Notes 1. H H / 2 is a "bad" incremental
    sequence, because it repeatedly compares the same
    values, and at the same time some values will not
    be compared to each other until H 1.
  • 2. Any incremental sequence of
    values of H can be used, as long as the last
    value is 1. Here are examples of "good"
    incremental sequences
  • H 3 H 1 gives the following incremental
    sequence
  • 1093, 364, 121, 40, 13, 4, 1.

4
Efficiency of Shell sort
  • Let the incremental sequence be H H / 2, foe
    example , 64, 32, 16, 8, 4, 2, 1.
  • Then
  • The number of repetitions of SegmentedInsertionSor
    t is O(Log N).
  • The outer loop is each SegmentedInsertionSort is
    O(N).
  • The inner loop of each SegmentedInsertionSort
    depends on the current order of the data within
    that segment.
  • Therefore, the total number of comparisons in
    this case is O(A N Log N),
  • where A is unknown.
  • Empirical results for a better incremental
    sequence, H 3 H 1, show the
  • average efficiency of Shell sort in terms of
    number of comparisons to be
  • O(N (log N)2), which is almost O(N1.5).

5
Advanced sorting Merge sort
  • The idea Given two files in ascending order, put
    them into a third file
  • also arranged in ascending order.
  • Example
  • file A file B
    file C
  • 3 1
    1
  • 7 5
    3
  • 9 8
    5
  • 12 10
    7
    The efficiency of this
  • 13 17
    8
    process is O(N)
  • 14 19
    9

  • 10

  • 12

  • 13

  • 14

  • 17

  • 19
  • The algorithm (let us call this procedure merge)
  • 1 Compare two numbers
  • 2 Transfer the smaller number

6
  • Algorithm merge (source, destination, lower, mid,
    upper)
  • Input source array, and a copy of it,
    destination
  • lower, mid and upper are integers
    defining sublists to be merged.
  • Output destination file sorted.
  • int s1 lower int s2 mid 1 int d
    lower
  • while (s1 lt mid or s2 ltupper)
  • if (precedes (sources1, sources2)
  • destinationd sources1 s1
    s1 1
  • else
  • destinationd sources2 s2
    s2 1
  • d d 1 // end if
  • // end while
  • if (s1 gt mid)
  • while (s2 lt upper)
  • destinationd sources2 s2 s2
    1 d d 1
  • else
  • while (s1 lt mid)
  • destinationd sources1 s1 s1
    1 d d 1

7
  • Note that merge takes two already sorted files.
    Therefore, we need another
  • procedure, mergeSort, to actually sort these
    files. mergeSort is a recursive
  • procedure, which at each step takes a file to be
    sorted, and produces two sorted
  • halves of this file. Because mergeSort
    continuously calls merge, and merge works
  • on two identical arrays, we must create a copy of
    original array, source, which we
  • will call destination.
  • Algorithm mergeSort (source, destination, lower,
    upper)
  • Input source array
  • a copy of source, destination
  • lower and upper are integers
    defining the current sublist to be sorted.
  • Output destination array sorted.
  • if (lower ltgt upper)
  • mid (lower upper) / 2
  • mergeSort (destination, source, lower,
    mid)
  • mergeSort (destination, source, mid1,
    upper)
  • merge (source, destination, lower, mid,
    upper)

8
Quick sort
  • The idea (assume the list of items to be sorted
    is represented as an array)
  • Select a data item, called the pivot, which will
    be placed in its proper place at the end of the
    current step. Remove it from the array.
  • Scan the array from right to left, comparing the
    data items with the pivot until an item with a
    smaller value is found. Put this item in the
    pivots place.
  • Scan the array from left to right, comparing data
    items with the pivot, and find the first item
    which is greater than the pivot. Place it in the
    position freed by the item moved at the previous
    step.
  • Continue alternating steps 2-3 until no more
    exchanges are possible. Place the pivot in the
    empty space, which is the proper place for that
    item.
  • Consider the sub-file to the left of the pivot,
    and repeat the same process.
  • Consider the sub-file to the right of the pivot,
    and repeat the same process.

9
Example
  • Consider the following list of items, and let the
    pivot be the leftmost item
  • Step 1
  • 15 8 7 3 2 14
    11 1 5 9 4 12 13
    6 10
  • 10 8 7 3 2 14
    11 1 5 9 4 12 13
    6 15
  • Step 2
  • 10 8 7 3 2 14
    11 1 5 9 4 12 13
    6 15
  • 6 8 7 3 2 14
    11 1 5 9 4 12
    13 ( ) 15
  • 6 8 7 3 2 (
    ) 11 1 5 9 4 12
    13 14 15
  • 6 8 7 3 2 4
    11 1 5 9 ( ) 12
    13 14 15
  • 6 8 7 3 2 4
    ( ) 1 5 9 11 12
    13 14 15

10
Example (contd.)
  • Step 3
  • 6 8 7 3 2 4
    9 1 5 10 11 12
    13 14 15
  • 5 8 7 3 2 4
    9 1 ( ) 10 11 12
    13 14 15
  • 5 ( ) 7 3 2 (
    ) 9 1 8 10 11 12
    13 14 15
  • 5 1 7 3 2 4
    9 ( ) 8 10 11 12
    13 14 15
  • 5 1 ( ) 3 2 4
    9 7 8 10 11 12
    13 14 15
  • 5 1 4 3 2 6
    9 7 8 10 11 12
    13 14 15
  • Step 4
  • 5 1 4 3 2 6
    9 7 8 10 11 12
    13 14 15
  • 2 1 4 3 ( )
    6 8 7 9 10 11 12
    13 14 15

11
Example (contd.)
  • Step 5
  • 2 1 4 3 5 6
    8 ( ) 9 10 11 12
    13 14 15
  • 1 ( ) 4 3 5
    6 7 8 9 10 11 12
    13 14 15
  • 1 2 4 3 5 6
    7 8 9 10 11 12
    13 14 15
  • Step 6
  • 1 2 4 3 5 6
    7 8 9 10 11 12
    13 14 15
  • 1 2 3 ( ) 5 6
    7 8 9 10 11 12
    13 14 15
  • 1 2 3 4 5 6
    7 8 9 10 11 12
    13 14 15

12
The partition method
  • Algorithm partition (A, lo, hi)
  • Input Array, A, of items to be sorted
  • lo and hi, integers defining
    the scope of the array to be sorted.
  • Output Assuming Alo to be a pivotal
    value, array A is returned in a
  • partitioned form, where
    pivotPoint is an index of the final destination
    of the
  • pivot
  • int pivot Alo
  • while (lo lt hi)
  • while (precedes (pivot, Ahi) (lo lt hi))
  • hi hi 1
  • if (hi ltgt lo)
  • Alo Ahi lo lo 1
  • while (precedes (Alo, pivot) (lo lt hi))
  • lo lo 1
  • if (hi ltgt lo)
  • Ahi Alo hi hi 1
  • // end while
  • Ahi pivot pivotPoint hi

13
The quickSort and sort procedures
  • Algorithm quickSort (A, lo, hi)
  • Input Array, A, of items to be sorted
  • lo and hi, integers defining
    the scope of the array to be sorted.
  • Output Array, A, sorted.
  • int pivotPoint partition (A, lo, hi)
  • if (lo lt pivotPoint)
  • quickSort (A, lo, pivotPoint-1)
  • if (hi gt pivotPoint)
  • quickSort (A, pivotPoint1, hi)
  • Algorithm Sort (A, N)
  • Input Array, A, of items to be sorted
  • integer N defining the number of
    items to be sorted.
  • Output Array, A, sorted.
  • quickSort (A, 1, N)

14
Example modified
  • Consider the same list as in the previous
    example, but let the pivot be the
  • rightmost item
  • Step 1
  • 15 8 7 3 2 14
    11 1 5 9 4 12 13
    6 10
  • 6 8 7 3 2 14
    11 1 5 9 4 12
    13 15 10
  • 6 8 7 3 2 4
    11 1 5 9 14 12
    13 15 10
  • 6 8 7 3 2 4
    9 1 5 11 14 12
    13 15 10
  • 6 8 7 3 2 4
    9 1 5 10 14 12
    13 15 11

15
Example modified (contd.)
  • Steps 2 - end
  • 6 8 7 3 2 4
    9 1 5 10 14 12 13
    15 11
  • 1 8 7 3 2 4
    9 6 5
  • 1 4 7 3 2 8
    9 6 5
  • 1 4 2 3 7 8
    9 6 5
  • 1 4 2 3 5 8
    9 6 7 10 11 12 13
    15 14
  • 1 2 4 3 5 6
    9 8 7 10 11 12 13
    14 15
  • 1 2 3 4 5 6
    7 8 9 10 11 12 13
    14 15

16
Static representation of the partitioning process
  • Example (original)

17
Static representation of the partitioning process
  • Example (modified)

10
11
5
3
7
2
1
18
Efficiency results
  • Result 1 The best case efficiency of quick sort
    is N log N (the pivot always divides the file in
    two equal halves).
  • Result 2 The worst case efficiency of quick sort
    is N2 (file already sorted).
  • Result 3 The average case efficiency of quick
    sort is 1.38 N log N. This result makes Quick
    sort good "general-purpose" sort. Its inner loop
    is very short, thus making Quick sort better
    compared to other N log N sorting methods.
  • Also Quick Sort is an "in-place" method, which
    uses only a small auxiliary
  • stack for recursion.
Write a Comment
User Comments (0)
About PowerShow.com