Radix Sort - PowerPoint PPT Presentation

About This Presentation
Title:

Radix Sort

Description:

Radix Sort. Unlike other methods, radix sort considers the structure of the keys. Assume that the keys are represented in base M number system (M == radix) ... – PowerPoint PPT presentation

Number of Views:819
Avg rating:3.0/5.0
Slides: 11
Provided by: thaddeusf
Category:
Tags: radix | sort

less

Transcript and Presenter's Notes

Title: Radix Sort


1
Radix Sort
  • CSC 172
  • SPRING 2004
  • LECTURE 25

2
Theoretical bound?
  • A n logn t
  • A 524,288 19 111
  • A 1.110-5
  • 1.110-5 134,217,728 27 40,380
  • 1.110-5 524,288 256 27/19 40,380
  • But we got 26,700 111 256

3
What happened?
  • The key word in the theoretical proof was
    comparison based
  • Some sorts are not comparison based

4
Radix Sort
  • Unlike other methods, radix sort considers the
    structure of the keys
  • Assume that the keys are represented in base M
    number system (M radix)
  • i.e. if M 2 we are in the binary number system
  • 910 10012
  • Sorting is done by comparing bits in the same
    position
  • This will extend to keys that are alphanumeric
    strings

5
Radix Exchange Sort
Sort array with respect to the leftmost bit
6
Radix Exchange Sort
Partition Array
Recursively sort subarrays Ignoring leftmost bit
7
Time
  • How many bits in the keys? b
  • How much work per bit? N
  • O(bN)
  • Not bad for 16 bit shorts

8
Exchange
  • In place, like Quicksort
  • Repeat
  • Scan top-down to find a 1
  • Scan bottom-up to find a 0
  • Exchange keys
  • Until scan indices cross

9
  • public static void mySorter(shortB)
  • int mask 0x8000
  • mySorter(B,0,B.length -1, mask)

10
  • public static void mySorter(short B,int
    bottom,int top, int mask)
  • if (mask lt 0 ) return
  • int oldtop top
  • int oldbottom bottom
  • while (top gt bottom)
  • while ((bottom lt (B.length - 1))
  • ((((int) Bbottom) mask) 0)) bottom
  • while ((top gt0) ((((int) Btop) mask) gt
    0)) top --
  • if (top gt bottom)
  • short temp Bbottom
  • Bbottom Btop
  • Btop temp
  • mySorter(B,oldbottom,top,maskgtgt1)
  • mySorter(B,top1,oldtop,maskgtgt1)
  • return
Write a Comment
User Comments (0)
About PowerShow.com