40 likes | 285 Vues
Bin/Radix Sort. Bin Sort Radix Sort. Bin Sort. for i 1 to n Insert(A[i],LIST[A[i].key]) for k FirstKey to LastKey Output(LIST[k]) Note that this is “cheating”. No comparisons, and we need to know all of the keys in advance. If the keys are: {0,1,2,3,4,5,6,7,8,9}, this
E N D
Bin/Radix Sort Bin Sort Radix Sort CS 303 – Bin/Radix Sort Lecture 18
Bin Sort • for i 1 to n Insert(A[i],LIST[A[i].key]) • for k FirstKey to LastKey Output(LIST[k]) • Note that this is “cheating”. No comparisons, and we need to know • all of the keys in advance. If the keys are: {0,1,2,3,4,5,6,7,8,9}, this • may be OK. If not...beware! • BinSort is O(n+m) – m is the number of possible keys CS 303 – Bin/Radix Sort Lecture 18
Radix Sort • If the Keys are of the form: f1f2...fn (for example YYMMDD) • Then, Radix Sort is simply: • Stable-Sort-on-fn() • ... • Stable-Sort-on-f2() • Stable-Sort-on-f1() • where a Stable-Sort is one where records with equal keys are • output in the SAME ORDER that they are input. • Describe card sorters. • Question: what Sorts are “Stable”? which ones can be made “Stable”? CS 303 – Bin/Radix Sort Lecture 18