Sorting2.9 - CountingSort Input: 1 0 0 1 3 1 1 3 1 0 2 1 0...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CountingSort Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0 Output: Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Value v: 0 5 # of records with digit v: N records. Time to count? CSE 2011 Prof. J. Elder - 96 - 1 9 2 3 3 2 (N) Last Updated: 4/1/10 11:16 AM CountingSort Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0 Output: Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 0 5 0 Value v: # of records with digit v: # of records with digit < v: 1 9 5 23 33 14 17 N records, k different values. Time to count? CSE 2011 Prof. J. Elder - 97 - (k) Last Updated: 4/1/10 11:16 AM CountingSort Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0 Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 3 3 Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Value v: 0 1 2 3 # of records with digit < v: 0 5 14 17 = location of first record with digit v. CSE 2011 Prof. J. Elder - 98 - Last Updated: 4/1/10 11:16 AM CountingSort Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0 1 Output: 0 ? Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Value v: Location of first record with digit v. 0 0 1 5 23 14 17 Algorithm: Go through the records in order putting them where they go. CSE 2011 Prof. J. Elder - 99 - Last Updated: 4/1/10 11:16 AM Loop Invariant The first i-1 keys have been placed in the correct locations in the output array The auxiliary data structure v indicates the location at which to place the ith key for each possible key value from [1..k-1]. CSE 2011 Prof. J. Elder - 100 - Last Updated: 4/1/10 11:16 AM ...
View Full Document

This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.

Page1 / 5

Sorting2.9 - CountingSort Input: 1 0 0 1 3 1 1 3 1 0 2 1 0...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online