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
- Nature of storage media
- fast memory
- secondary storage
- Random versus sequential access of the data
- Space
- can we use space in excess of the space occupied by the data?
- 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:
- use linked lists
• onlyn ·(n –1)/2 comparisons
- 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:
- search in increasing order
- Ex: insert 411 in [113 400 612 818]
- 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:
- count frequency of each value
- calculate the destination address for each value by making a pass over the array of counters in reverse order and reuse the counter field
- 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
- 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
- apply same technique with successively shorter increments
- 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