0 votes 0 votes counting sort assumes that each of the n input is an integer in the range 0 to k, for some integer k. please explain when k=O(n), the sort run in O(n) time. Algorithms cormen sorting time-complexity + – shebya nautiyal asked Apr 7, 2017 • edited Jun 24, 2022 by makhdoom ghaya shebya nautiyal 429 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
1 votes 1 votes Suppose we have n keys that are non-negative integers such that the biggest key, k, is not much bigger than the number of keys, i.e. k = O(n). Take three arrays, A, B, and C. A is the input array with n keys. B is the sorted output array, also of length n. C will be our counting array of length k + 1. then Apply counting sort algorithm Since k = O(n), each for loop in algorithm does about n things, so counting sort has running time O(n). For more info refer here http://www.cs.otago.ac.nz/cosc242/pdf/lecture9.pdf http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap09.htm Shubham Sharma 2 answered Apr 7, 2017 Shubham Sharma 2 comment Share Follow See 1 comment See all 1 1 comment reply shebya nautiyal commented Apr 7, 2017 reply Follow Share thnxs :) 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes Counting sort Time complexity T(n)=O(n + k) , where n=number of elements in the array. k=range of the number. Hence ,when k=O(n) running time becomes O(n). Deepak Gupta 1 answered Apr 8, 2017 Deepak Gupta 1 comment Share Follow See all 0 reply Please log in or register to add a comment.