Sorting in Linear Time How fast can we sort? Decision-tree example Decision-tree example Decision-tree example Decision-tree example Decision-tree example Decision-tree example Lower Bound for decision-tree sorting Lower bound for comparison sorting Sorting in linear time Counting sort COUNTING-SORT(A,B,k) // Initialize aux. store // Count how many times occurred. // Cumulative sums of aux. store // ??? Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Counting-sort example Analysis Running Time Stable sorting Counting sort array size limitation problem • If we need sort 32 bits integers, size of count array 232 = 4GB ! • Unsorted array size is usually smaller than this (1KB << 4GB) • Solution... Use smaller parts like digits and sort these parts particularly : Radix sort Radix sort Operation of radix sort Analysis of radix sort • Given n d-digit number in which each digit can take on up to k-possible values, the running time of RADIX-SORT sorts these numbers is (d*(n+k)) Bucket Sort BUCKET - SORT(A) 1 n lengthA 2 for i 1 to n 3 do insert Ai into list B nAi 4 for i 0 to n 1 5 do sort list Bi with insertion sort 6 concatanete the lists B0, B1,..., Bn 1 together in order Bucket Sort A B 1 .78 0 2 .17 1 .12 .17 3 .39 2 .21 .23 4 .26 3 5 .72 4 6 .94 5 7 .21 6 .68 8 .12 7 .72 9 .23 8 10 .68 9 .94 / .26 / .78 / / / Bucket Sort Running time: n 1 T (n) (n) O(n ) i 0 2 i Order Statistics
© Copyright 2026 Paperzz