{"id":25188,"date":"2017-10-15T14:49:54","date_gmt":"2017-10-15T09:19:54","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25188"},"modified":"2017-10-15T14:49:54","modified_gmt":"2017-10-15T09:19:54","slug":"counting-sort","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/counting-sort\/","title":{"rendered":"Counting Sort"},"content":{"rendered":"<p>Counting Sort is a sorting technique based on keys between a specific range.<span id=\"more-115936\"><\/span> It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence.<\/p>\n<p>Let us understand it with the help of an example.<\/p>\n<pre>For simplicity, consider the data in the range 0 to 9. \r\nInput data: 1, 4, 1, 2, 7, 5, 2\r\n  1) Take a count array to store the count of each unique object.\r\n  Index:     0  1  2  3  4  5  6  7  8  9\r\n  Count:     0  2  2  0   1  1  0  1  0  0\r\n\r\n  2) Modify the count array such that each element at each index \r\n  stores the sum of previous counts. \r\n  Index:     0  1  2  3  4  5  6  7  8  9\r\n  Count:     0  2  4  4  5  6  6  7  7  7\r\n\r\nThe modified count array indicates the position of each object in \r\nthe output sequence.\r\n \r\n  3) Output each object from the input sequence followed by \r\n  decreasing its count by 1.\r\n  Process the input data: 1, 4, 1, 2, 7, 5, 2. Position of 1 is 2.\r\n  Put data 1 at index 2 in output. Decrease count by 1 to place \r\n  next data 1 at an index 1 smaller than this index.\r\n<\/pre>\n[ad type=\u201dbanner\u201d]\n<p>Following is C implementation of counting sort.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%20Program%20for%20counting%20sort%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%23define%20RANGE%20255%0A%20%0A%2F%2F%20The%20main%20function%20that%20sort%20the%20given%20string%20arr%5B%5D%20in%0A%2F%2F%20alphabatical%20order%0Avoid%20countSort(char%20arr%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20The%20output%20character%20array%20that%20will%20have%20sorted%20arr%0A%20%20%20%20char%20output%5Bstrlen(arr)%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20a%20count%20array%20to%20store%20count%20of%20inidividul%0A%20%20%20%20%2F%2F%20characters%20and%20initialize%20count%20array%20as%200%0A%20%20%20%20int%20count%5BRANGE%20%2B%201%5D%2C%20i%3B%0A%20%20%20%20memset(count%2C%200%2C%20sizeof(count))%3B%0A%20%0A%20%20%20%20%2F%2F%20Store%20count%20of%20each%20character%0A%20%20%20%20for(i%20%3D%200%3B%20arr%5Bi%5D%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%2B%2Bcount%5Barr%5Bi%5D%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Change%20count%5Bi%5D%20so%20that%20count%5Bi%5D%20now%20contains%20actual%0A%20%20%20%20%2F%2F%20position%20of%20this%20character%20in%20output%20array%0A%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%3D%20RANGE%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20count%5Bi%5D%20%2B%3D%20count%5Bi-1%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Build%20the%20output%20character%20array%0A%20%20%20%20for%20(i%20%3D%200%3B%20arr%5Bi%5D%3B%20%2B%2Bi)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20output%5Bcount%5Barr%5Bi%5D%5D-1%5D%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20\u2013count%5Barr%5Bi%5D%5D%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Copy%20the%20output%20array%20to%20arr%2C%20so%20that%20arr%20now%0A%20%20%20%20%2F%2F%20contains%20sorted%20characters%0A%20%20%20%20for%20(i%20%3D%200%3B%20arr%5Bi%5D%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20output%5Bi%5D%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20char%20arr%5B%5D%20%3D%20%22geeksforgeeks%22%3B%2F%2F%22applepp%22%3B%0A%20%0A%20%20%20%20countSort(arr)%3B%0A%20%0A%20%20%20%20printf(%22Sorted%20character%20array%20is%20%25s%5Cn%22%2C%20arr)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dc\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>JAVA<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20implementation%20of%20Counting%20Sort%0Aclass%20CountingSort%0A%7B%0A%20%20%20%20void%20sort(char%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20The%20output%20character%20array%20that%20will%20have%20sorted%20arr%0A%20%20%20%20%20%20%20%20char%20output%5B%5D%20%3D%20new%20char%5Bn%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Create%20a%20count%20array%20to%20store%20count%20of%20inidividul%0A%20%20%20%20%20%20%20%20%2F%2F%20characters%20and%20initialize%20count%20array%20as%200%0A%20%20%20%20%20%20%20%20int%20count%5B%5D%20%3D%20new%20int%5B256%5D%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3C256%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20count%5Bi%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20store%20count%20of%20each%20character%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%2Bcount%5Barr%5Bi%5D%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Change%20count%5Bi%5D%20so%20that%20count%5Bi%5D%20now%20contains%20actual%0A%20%20%20%20%20%20%20%20%2F%2F%20position%20of%20this%20character%20in%20output%20array%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D1%3B%20i%3C%3D255%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20count%5Bi%5D%20%2B%3D%20count%5Bi-1%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Build%20the%20output%20character%20array%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20output%5Bcount%5Barr%5Bi%5D%5D-1%5D%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20\u2013count%5Barr%5Bi%5D%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Copy%20the%20output%20array%20to%20arr%2C%20so%20that%20arr%20now%0A%20%20%20%20%20%20%20%20%2F%2F%20contains%20sorted%20characters%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20output%5Bi%5D%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20CountingSort%20ob%20%3D%20new%20CountingSort()%3B%0A%20%20%20%20%20%20%20%20char%20arr%5B%5D%20%3D%20%7B\u2019g\u2019%2C%20\u2019e\u2019%2C%20\u2019e\u2019%2C%20\u2019k\u2019%2C%20\u2019s\u2019%2C%20\u2019f\u2019%2C%20\u2019o\u2019%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20\u2019r\u2019%2C%20\u2019g\u2019%2C%20\u2019e\u2019%2C%20\u2019e\u2019%2C%20\u2019k\u2019%2C%20\u2019s\u2019%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20ob.sort(arr)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.print(%22Sorted%20character%20array%20is%20%22)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Carr.length%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201djava\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>PYTHON<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20for%20counting%20sort%0A%20%0A%23%20The%20main%20function%20that%20sort%20the%20given%20string%20arr%5B%5D%20in%20%0A%23%20alphabetical%20order%0Adef%20countSort(arr)%3A%0A%20%0A%20%20%20%20%23%20The%20output%20character%20array%20that%20will%20have%20sorted%20arr%0A%20%20%20%20output%20%3D%20%5B0%20for%20i%20in%20range(256)%5D%0A%20%0A%20%20%20%20%23%20Create%20a%20count%20array%20to%20store%20count%20of%20inidividul%0A%20%20%20%20%23%20characters%20and%20initialize%20count%20array%20as%200%0A%20%20%20%20count%20%3D%20%5B0%20for%20i%20in%20range(256)%5D%0A%20%0A%20%20%20%20%23%20For%20storing%20the%20resulting%20answer%20since%20the%20%0A%20%20%20%20%23%20string%20is%20immutable%0A%20%20%20%20ans%20%3D%20%5B%22%22%20for%20_%20in%20arr%5D%0A%20%0A%20%20%20%20%23%20Store%20count%20of%20each%20character%0A%20%20%20%20for%20i%20in%20arr%3A%0A%20%20%20%20%20%20%20%20count%5Bord(i)%5D%20%2B%3D%201%0A%20%0A%20%20%20%20%23%20Change%20count%5Bi%5D%20so%20that%20count%5Bi%5D%20now%20contains%20actual%0A%20%20%20%20%23%20position%20of%20this%20character%20in%20output%20array%0A%20%20%20%20for%20i%20in%20range(256)%3A%0A%20%20%20%20%20%20%20%20count%5Bi%5D%20%2B%3D%20count%5Bi-1%5D%0A%20%0A%20%20%20%20%23%20Build%20the%20output%20character%20array%0A%20%20%20%20for%20i%20in%20range(len(arr))%3A%0A%20%20%20%20%20%20%20%20output%5Bcount%5Bord(arr%5Bi%5D)%5D-1%5D%20%3D%20arr%5Bi%5D%0A%20%20%20%20%20%20%20%20count%5Bord(arr%5Bi%5D)%5D%20-%3D%201%0A%20%0A%20%20%20%20%23%20Copy%20the%20output%20array%20to%20arr%2C%20so%20that%20arr%20now%0A%20%20%20%20%23%20contains%20sorted%20characters%0A%20%20%20%20for%20i%20in%20range(len(arr))%3A%0A%20%20%20%20%20%20%20%20ans%5Bi%5D%20%3D%20output%5Bi%5D%0A%20%20%20%20return%20ans%20%0A%20%0A%23%20Driver%20program%20to%20test%20above%20function%0Aarr%20%3D%20%22geeksforgeeks%22%0Aans%20%3D%20countSort(arr)%0Aprint%20%22Sorted%20character%20array%20is%20%25s%22%20%20%25(%22%22.join(ans))%0A%20%0A\u201d message=\u201dpython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Sorted character array is eeeefggkkorss<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n+k) where n is the number of elements in input array and k is the range of input.<br \/>\n<strong>Auxiliary Space:<\/strong> O(n+k)<\/p>\n<p><strong>Points to be noted:<\/strong><br \/>\n<strong>1.<\/strong> Counting sort is efficient if the range of input data is not significantly greater than the number of objects to be sorted. Consider the situation where the input sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.<br \/>\n<strong>2.<\/strong> It is not a comparison based sorting. It running time complexity is O(n) with space proportional to the range of data.<br \/>\n<strong>3.<\/strong> It is often used as a sub-routine to another sorting algorithm like radix sort.<br \/>\n<strong>4.<\/strong> Counting sort uses a partial hashing to count the occurrence of the data object in O(1).<br \/>\n<strong>5.<\/strong> Counting sort can be extended to work for negative inputs also.<\/p>\n<p><strong>Exercise:<\/strong><br \/>\n<strong>1.<\/strong> Modify above code to sort the input data in the range from M to N.<br \/>\n<strong>2.<\/strong> Modify above code to sort negative input data.<br \/>\n<strong>3.<\/strong> Is counting sort stable and online?<br \/>\n<strong>4. <\/strong>Thoughts on parallelizing the counting sort algorithm.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Counting sort &#8211; Searching and Sorting &#8211; Counting Sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing).<\/p>\n","protected":false},"author":1,"featured_media":25191,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,71670],"tags":[71894,71852,71847,71840,71893,70263,70268,70461,71121,71879,71122,71884,71874,71877,70780,71833,71869,71891,70497,71868,71885,71875,70271,70928,70017,71225,71881,70262,71262,71872,71889,71289,71892,71886,71878,71267,70046,71145,70977,71266,71870,71844,71857,71858,71846,71843,71880,71120,70016,71883,71697,71873,71890,71674,71308,71125,71695,71871,70020,71882,70652,70019,71887,71876,71888],"class_list":["post-25188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithm","category-searching-and-sorting","tag-71894","tag-algorithm-for-radix-sort","tag-algorithm-for-radix-sort-in-data-structure","tag-algorithm-of-radix-sort","tag-algorithm-sort","tag-binary-search-tree","tag-binary-tree","tag-bubble-sort","tag-bubble-sort-algorithm","tag-bubble-sort-concept","tag-bubble-sort-java","tag-bubble-sort-online","tag-bubble-sort-technique","tag-bubble-sort-theory","tag-bubblesort","tag-comb-sort","tag-count-python","tag-counting-algorithm","tag-counting-sort","tag-counting-sort-algorithm","tag-decision-tree-sorting","tag-example-of-counting-sort","tag-heap-sort","tag-heap-sort-algorithm","tag-insertion-sort-algorithm","tag-insertion-sort-program","tag-math-sort","tag-merge-sort","tag-merge-sort-algorithm","tag-merge-sort-algorithm-c-code","tag-merge-sort-algorithm-explanation","tag-merge-sort-algorithm-in-c","tag-merge-sort-algorithm-java","tag-pseudocode-for-sorting-algorithms","tag-python-sorting-algorithms","tag-quick-sort-algorithm","tag-quicksort","tag-quicksort-c","tag-quicksort-java","tag-radix-sort-algorithm","tag-radix-sort-algorithm-in-data-structure","tag-radix-sort-algorithm-in-java","tag-radix-sort-algorithm-pseudocode","tag-radix-sort-analysis","tag-radix-sort-applications","tag-radix-sort-code-java","tag-radix-sort-in-c","tag-selection-sort","tag-selection-sort-algorithm","tag-selection-sort-simulation","tag-shell-sort","tag-short-bubble-sort","tag-simple-merge-sort-algorithm","tag-sort","tag-sort-array","tag-sort-code","tag-sorted","tag-sorted-python","tag-sorting-algorithms","tag-sorting-algorithms-explained-with-examples","tag-sorting-algorithms-in-c","tag-sorting-algorithms-in-java","tag-sorting-by-counting","tag-sorting-code-in-c","tag-what-is-merge-sort-algorithm"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25188"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25188\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/25191"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}