Shell Sort - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Shell Sort

Description:

compares distant items first and works way down to nearby items ... Comparing O(N2), O(N1.25) & O(N) The family of sorting methods. Main sorting themes ... – PowerPoint PPT presentation

Number of Views:306
Avg rating:3.0/5.0
Slides: 15
Provided by: dsol
Category:
Tags: comparing | shell | sort

less

Transcript and Presenter's Notes

Title: Shell Sort


1
Shell Sort
  • CSET 3150

2
Sorting and Searching
  • Shell Sort
  • more efficient than Bubble, Insertion and
    Selection sorts
  • compares distant items first and works way down
    to nearby items
  • interval is called the gap
  • in a simple implementation, gap begins at
    one-half the length of the list and is
    successively halved until each item is compared
    with neighbor

3
Shell Sort
  • A variation of the insertion sort
  • But faster than O(n2)
  • Done by sorting subarrays of equally spaced
    indices
  • Instead of moving to an adjacent location an
    element moves several locations away
  • Results in an almost sorted array
  • This array sorted efficiently with ordinary
    insertion sort

4
Shell Sort
  • Given an array of n items
  • Start with the first item, and then take every
    n/2 items to form a sub- array
  • Take the second item and every n/2 item from
    there to form your second sub-array
  • Continue to form sub-arrays until every item is
    included in a sub-array
  • When you have all of your sub-arrays sort them by
    an insertion sort
  • Then take the first elements of the sorted
    subarrays as the first elements in a new big
    array the second elements as the next group
    etc.
  • Form new subarrays by selecting every (n/2)/2
    items and then process as before
  • Continue until you select 1 item

5
Shell Sort
An array and the subarrays formed by grouping
elements whose indices are 6 apart.
6
Shell Sort
The subarrays of the previous figure after they
are sorted, and the array that contains them.
7
Shell Sort
The subarrays of the array in previous figure
formed by grouping elements whose indices are 3
apart
8
Shell Sort
The subarrays of previous figure after they are
sorted, and the array that contains them.
9
Another Shell Sort Example
  • Consider sorting the following list by Shell sort

GAP3
GAP2
GAP1
9 24 45 91
6 8 23
4 7 12
9 6 4 24 8 7 45 23 12 91
4 8 9 12 45
6 7 23 24 91
4 6 8 7 9 23 12 24 45 91
45 23 4 9 6 7 91 8 12 24
4 6 7 8 9 12 23 24 45 91
10
Choosing the gap size
  • The idea of the decreasing gap size is that the
    list becomes more and more sorted each time the
    gap size is reduced, therefore you dont (for
    example) want to have a gap size of 4 followed by
    a gap size of 2 because youll be sorting half
    the numbers a second time.
  • There is no formal proof of a good initial gap
    size, but about a 10th the size of N is a
    reasonable start.
  • Try to use prime numbers as your gap size, or odd
    numbers if you can not readily get a list of
    primes (though note gaps of 9, 7, 5, 3, 1 will
    be doing less work when gap3).

11
Efficiency of Shell Sort
  • Efficiency is O(n2) for worst case
  • If n is a power of 2
  • Average-case behavior is O(n1.5)
  • Any time the variable space is even, add 1
  • This also results in O(n1.5)

12
Comparing the Algorithms
Best Average Worst Case
Case Case Selection sort O(n2) O(n2)
O(n2) Insertion sort O(n) O(n2)
O(n2) Shell sort O(n) O(n1.5) O(n1.5)
The time efficiencies of three sorting algorithms
13
Comparing O(N2), O(N1.25) O(N)
14
The family of sorting methods
Main sorting themes
Address- -based sorting
Comparison-based sorting
Proxmap Sort
RadixSort
Transposition sorting
BubbleSort
Divide and conquer
Diminishing increment sorting
Priority queue sorting
Insert and keep sorted
MergeSort
QuickSort
Selection sort
Heap sort
Insertion sort
Tree sort
ShellSort
Write a Comment
User Comments (0)
About PowerShow.com