Data Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Data Structures

Description:

Data Structures & Algorithms Radix Sort Often the key has structure Often only part of the key is needed to decide the order Processing one piece of the key at a time ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 11
Provided by: nemo68
Learn more at: https://www.cise.ufl.edu
Category:

less

Transcript and Presenter's Notes

Title: Data Structures


1
Data Structures Algorithms Radix Sort
2
Key Comparison
  • Often the key has structure
  • Often only part of the key is needed to decide
    the order
  • Processing one piece of the key at a time is
    radix sort
  • Bytes of binary numbers
  • Characters in strings
  • Digits in decimal numbers
  • Advantage from limited range of each piece!

3
Radix Sorting
Treat keys as numbers represented in a base-R
system for various values of R (the radix)
Partially sort based on each piece Decimal
numbers R 10 Binary numbers R 2k Character
strings R 256 or 128 Pretty much anything
bits Fortunately, C and C allow easy access to
bits of representations....
4
Radix Sort Approaches
  • MSD Most Significant Digit
  • a.k.a. BucketSort or BinSort
  • Examine keys in left-to-right order
  • Work with most significant digits first
  • Partially sort (a la QuickSort)
  • Examine the minimum amount of information to get
    the sort done
  • LSD Least Significant Digit
  • Traditional RadixSort
  • Examine keys in right-to-left order
  • Work with least significant digits first

5
Getting at the Digits
If string or array, just use array index bth
part is just ab, or aN-b If binary integer,
bth digit of number a is a/Rb R If real
between 0 and 1 bth digit of number a is aRb
R
6
BucketSort Example
1A
09
03
25
3C
17
13
39
24
18
1A
09
03
18
13
17
3C
39
24
25
100000
03
09
1A
18
13
17
25
24
39
3C
010000
03
09
25
24
39
3C
17
13
18
1A
001000
03
09
13
17
18
1A
24
25
39
3C
000100
03
09
13
17
18
1A
24
25
39
3C
000010
03
09
13
17
18
1A
24
25
39
3C
000001
7
MSD Radix Sort Tree
ALL
gt1F
lt20
gt2F
gt0F
lt30
lt10
gt37
gt27
gt17
gt7
lt38
lt18
lt8
lt28
gt13
lt3A
lt14
gt1B
gt3B
lt1C
09
03
gt23
lt24
gt13
39
lt14
13
17
3C
gt24
lt25
1A
18
24
25
8
LSD Radix Sort
  • Work from right to left, starting with least
    significant digit
  • Not intuitively obvious it works...
  • Requires a stable sort to work
  • Correctness
  • After items in order on i LSBs (in stable
    manner) two keys are in order based on key
    digits examined so far
  • First digit is different put into order this
    pass
  • First digit same stability

9
LSD Radix Sort
  • Work from right to left, starting with least
    significant digit
  • Not intuitively obvious it works...
  • Requires a stable sort to work
  • Correctness
  • After items in order on i LSDs
  • w-i MSDs same gt stability keeps order
  • w-i MSDs differ gt later pass will sort

10
LSD RadixSort Example
1A
09
03
25
3C
17
13
39
24
18
1A
09
03
25
3C
17
13
39
24
18
000001
1A
09
03
25
3C
17
13
39
24
18
000010
1A
09
03
25
3C
17
13
39
24
18
000100
09
25
17
39
18
1A
03
3C
13
24
001000
1A
09
03
25
3C
17
13
39
24
18
010000
1A
09
03
25
3C
17
13
39
24
18
100000
11
Radix Sort Performance
  • Takes time proportional to passes
  • Takes time proportional to elements
  • Total time O(Nw) where w is the number of
    digits (key pieces)
  • Binary QuickSort examines about N lg N bits, on
    average, when sorting keys of random bits
Write a Comment
User Comments (0)
About PowerShow.com