Sorting part 2 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Sorting part 2

Description:

The original problem partitioned into simpler sub-problems, ... Radix Sort. Approach. 1. Decompose key C into components C1, C2, ... Cd ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 18
Provided by: bernar8
Learn more at: http://www.cs.gsu.edu
Category:
Tags: part | radix | sorting

less

Transcript and Presenter's Notes

Title: Sorting part 2


1
Sorting part 2
  • Dr. Bernard Chen Ph.D.
  • University of Central Arkansas
  • Fall 2008

2
Quicksort
  • Quicksort uses a divide-and-conquer strategy
  • A recursive approach
  • The original problem partitioned into simpler
    sub-problems,
  • Each sub problem considered independently.
  • Subdivision continues until sub problems obtained
    are simple enough to be solved directly

3
Quicksort
  • Choose some element called a pivot
  • Perform a sequence of exchanges so that
  • All elements that are less than this pivot are to
    its left and
  • All elements that are greater than the pivot are
    to its right.

4
Quicksort
  • If the list has 0 or 1 elements,
  • return. // the list is sorted
  • Else do
  • Pick an element in the list to use as the pivot.
  •   Split the remaining elements into two disjoint
    groups
  • SmallerThanPivot all elements lt pivot
  • LargerThanPivot all elements gt pivot
  •  
  •  Return the list rearranged as
  • Quicksort(SmallerThanPivot),
  • pivot,
  • Quicksort(LargerThanPivot).

5
Quicksort
  • Given to sort75, 70, 65, 84, 98, 78, 100, 93,
    55, 61, 81, 68
  • Select, arbitrarily, the first element, 75, as
    pivot.
  • Search from right for elements lt 75, stop at
    first element lt75
  • Search from left for elements gt 75, stop at first
    element gt75
  • Swap these two elements, and then repeat this
    process

6
Quicksort Example
  • 75, 70, 65, 68, 61, 55, 100, 93, 78, 98, 81, 84
  • When done, swap with pivot
  • This SPLIT operation placed pivot 75 so that all
    elements to the left were lt 75 and all elements
    to the right were gt75.
  • View code for split() template
  • 75 is now placed appropriately
  • Need to sort sublists on either side of 75

7
Quicksort Example
  • Need to sort (independently)
  • 55, 70, 65, 68, 61 and 100, 93, 78, 98, 81,
    84
  • Let pivot be 55, look from each end for values
    larger/smaller than 55, swap
  • Same for 2nd list, pivot is 100
  • Sort the resulting sublists in the same manner
    until sublist is trivial (size 0 or 1)
  • View quicksort() recursive function

8
Quicksort Split
  • int split (ElementType x, int first, int last)
  • ElementType pivot xfirst
  • int left first, right last
  • While (left lt right)
  • while (pivot lt xright)
  • right--
  • while (left lt right xleftltpivot)
  • left
  • if (left lt right)
  • swap(xleft,xright)
  • int pos right
  • xfirst xpos
  • xpos pivot
  • return pos

9
Quicksort
  • Note visual example ofa quicksort on an array

etc.
10
Quicksort Performance
  • O(log2n) is the average case computing time
  • If the pivot results in sublists of approximately
    the same size.
  • O(n2) worst-case
  • List already ordered, elements in reverse
  • When Split() repetitively results, for example,
    in one empty sublist

11
Improvements to Quicksort
  • Better method for selecting the pivot is the
    median-of-three rule,
  • Select the median of the first, middle, and last
    elements in each sublist as the pivot.
  • Often the list to be sorted is already partially
    ordered
  • Median-of-three rule will select a pivot closer
    to the middle of the sublist than will the
    first-element rule.

12
Counting Sort
  • Counting sort assumes that each of the n input
    elements is an integer in the range 0 to k
  • When kO(n), the sort runs in O(n) time

13
Counting Sort
  • Approach
  • Sorts keys with values over range 0..k
  • Count number of occurrences of each key
  • Calculate of keys lt each key
  • Place keys in sorted location using keys counted

14
(No Transcript)
15
(No Transcript)
16
Radix Sort
  • Approach
  • 1. Decompose key C into components C1, C2, Cd
  • Component d is least significant, Each component
    has values over range 0..k

17
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com