Insertion Sort and Radix Sort Algorithms - Prof. Hanan Samet, Study notes of Data Structures and Algorithms

An overview of two sorting algorithms: insertion sort and radix sort. Insertion sort is a simple sorting algorithm that builds a sorted list one item at a time. Radix sort is a non-comparison-based sorting algorithm that sorts data based on the value of their digits or bits. The document also discusses the properties and advantages of each algorithm.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-pwo
koofers-user-pwo 🇺🇸

3

(1)

10 documents

1 / 79

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
st0
Copyright © 1997 Hanan Samet
These notes may not be reproduced by any means (mechanical or elec-
tronic or any other) without the express written permission of Hanan Samet
SORTING TECHNIQUES
Hanan Samet
Computer Science Department and
Center for Automation Research and
Institute for Advanced Computer Studies
University of Maryland
College Park, Maryland 20742
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f

Partial preview of the text

Download Insertion Sort and Radix Sort Algorithms - Prof. Hanan Samet and more Study notes Data Structures and Algorithms in PDF only on Docsity!

st

Copyright © 1997 Hanan Samet

These notes may not be reproduced by any means (mechanical or elec- tronic or any other) without the express written permission of Hanan Samet

SORTING TECHNIQUES

Hanan Samet

Computer Science Department and Center for Automation Research and Institute for Advanced Computer Studies University of Maryland College Park, Maryland 20742 e-mail: [email protected]

st

ISSUES

  1. Nature of storage media
    • fast memory
    • secondary storage
  2. Random versus sequential access of the data
  3. Space
    • can we use space in excess of the space occupied by the data?
  4. Stability
    • is the relative order of duplicate occurrences of a data value preserved?
    • useful as adds a dimension of priority (e.g., time stamps in a scheduling application) for free

Copyright © 1998 by Hanan Samet

(^1) st SELECTION SORT b

  • Find the smallest element and output it 113 400 818 612 411 113 113 113 113 113

Copyright © 1998 by Hanan Samet

(^2) st r

Copyright © 1998 by Hanan Samet

(^1) st SELECTION SORT b

  • Find the smallest element and output it 113 400 818 612 411 113 113 113 113 113

Copyright © 1998 by Hanan Samet

(^2) st r

Copyright © 1998 by Hanan Samet

(^3) st z

Copyright © 1998 by Hanan Samet

(^1) st SELECTION SORT b

  • Find the smallest element and output it 113 400 818 612 411 113 113 113 113 113

Copyright © 1998 by Hanan Samet

(^2) st r

Copyright © 1998 by Hanan Samet

(^3) st z

Copyright © 1998 by Hanan Samet

(^4) st g

Copyright © 1998 by Hanan Samet

(^5) st v

Copyright © 1998 by Hanan Samet

(^1) st SELECTION SORT b

  • Find the smallest element and output it 113 400 818 612 411 113 113 113 113 113

Copyright © 1998 by Hanan Samet

(^2) st r

Copyright © 1998 by Hanan Samet

(^3) st z

Copyright © 1998 by Hanan Samet

(^4) st g

Copyright © 1998 by Hanan Samet

(^5) st v

Copyright © 1998 by Hanan Samet

(^6) st b

Step 1 causes no change as 113 is the smallest element

Step 2 causes 311 in position 6 to be exchanged with 400 in position 2 resulting in the first instance of 400 becoming the second instance

  • In-place variant can be made stable:
    1. use linked lists

• onlyn ·(n –1)/2 comparisons

  1. exchange adjacent elements

• O (n 2 ) time as alwaysn ·(n –1) comparisons

  • Needs extra space for output
  • In-place variant is not stable
  • Ex: 1 113

1 2

Copyright © 1998 by Hanan Samet

st INSERTION SORT

  • Values on the input list are taken in sequence and put in their proper position in the output list
  • Previously moved values may have to be moved again
  • Makes only one pass on the input list at cost of move operations

1 b

Copyright © 1998 by Hanan Samet

st INSERTION SORT

  • Values on the input list are taken in sequence and put in their proper position in the output list
  • Previously moved values may have to be moved again
  • Makes only one pass on the input list at cost of move operations

1 b

Copyright © 1998 by Hanan Samet

(^2) st r

Copyright © 1998 by Hanan Samet

st INSERTION SORT

  • Values on the input list are taken in sequence and put in their proper position in the output list
  • Previously moved values may have to be moved again
  • Makes only one pass on the input list at cost of move operations

1 b

Copyright © 1998 by Hanan Samet

(^2) st r

Copyright © 1998 by Hanan Samet

(^3) st z

Copyright © 1998 by Hanan Samet

(^4) st g

Copyright © 1998 by Hanan Samet

st INSERTION SORT

  • Values on the input list are taken in sequence and put in their proper position in the output list
  • Previously moved values may have to be moved again
  • Makes only one pass on the input list at cost of move operations

1 b

Copyright © 1998 by Hanan Samet

(^2) st r

Copyright © 1998 by Hanan Samet

(^3) st z

Copyright © 1998 by Hanan Samet

(^4) st g

Copyright © 1998 by Hanan Samet

(^5) st v

Copyright © 1998 by Hanan Samet

st

MECHANICS OF INSERTION

• Assume insertingx, thei th^ element

  • Must search for position to make the insertion
  • Two methods:
    1. search in increasing order
      • Ex: insert 411 in [113 400 612 818]
    2. search in decreasing order
      • Ex: insert 411 in [113 400 612 818]

Copyright © 1998 by Hanan Samet

st

SEARCH IN INCREASING ORDER

  • Ex: insert 411 in [113 400 612 818]

• Initialize the content of the output positioni to ∞ so that

we don’t have to test if we are at end of current list

• Start atj=1 and find first positionj wherex belongs — i.e.,

x < out[j ]

• Move all elements at positionsk (k ≥ j) one to right (i.e., to

k+1)

• j comparisons andi – j+1 moves

• Total ofi+1 operations for stepi

• Total of (n+1)(n+2)/2–1 =n(n+3)/2 operations

independent of whether the file is initially sorted, not sorted, or sorted in reverse order

Copyright © 1998 by Hanan Samet

st

SORTING BY DISTRIBUTION COUNTING

  • Insertion sort must perform move operations because when a value is inserted in the output list, we don’t know its final position
  • If we know the distribution of the various values, then we can calculate their final position in advance
  • If range of values is finite, use an extra array of counters
  • Algorithm:
    1. count frequency of each value
    2. calculate the destination address for each value by making a pass over the array of counters in reverse order and reuse the counter field
    3. place the values in the right place in the output list

• O (m +n ) time forn values in the range 0 …m –1 and stable

  • Not in-place because on the final step we can’t tell if a value is in its proper location as values are not inserted in the output list in sorted order
  • Ex:

input 0 1 2 3 4 5 6 7 list: 3a 0a 8a 2a 1a 1b 2b 0b

counter: 0 1 2 3 4 5 6 7 8 9 value: 2 2 2 1 0 0 0 0 1 0 position: 0 2 4 6 7 7 7 7 7 8

output 0 1 2 3 4 5 6 7 list: 0a 0b 1a 1b 2a 2b 3a 8a

Copyright © 1998 by Hanan Samet

st SHELLSORT

  • Drawback of insertion and bubble sort is that move operations only involve adjacent elements
  • If largest element is initially at the start, then by the end of

the sort it will have been the subject ofn move or

exchange operations

  • Can reduce number of move operations by allowing values to make larger jumps
  • Sort successively larger sublists
    1. e.g., sort every fourth element
      • elements 1, 5, 9, 13, 17 form one sorted list
      • elements 2, 6, 10, 14, 18 form a second sorted list
      • elements 3, 7, 11, 15, 19 form a third sorted list
      • elements 4, 8, 12, 16, 20 form a fourth sorted list
    2. apply same technique with successively shorter increments
    3. final step uses an increment of size 1
  • Usually implement by using insertion sort (that searches for position to make the insertion in decreasing order) at each step
  • Ex: with increment sequence of 4, 2, 1

1 b

start 113 400 818 612 411 311 412 420

Copyright © 1998 by Hanan Samet