Advertisement
mehedi1

Counting sort (Corman).cpp

Jul 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int mx = 0;
  4.  
  5. void countSort(int A[], int B[], int n) {
  6.     int C[mx+1]={0};
  7.     for(int i=1;i<=n;++i) { //fequency counting
  8.         C[A[i]]++;
  9.     }
  10.     for(int i=1;i<=mx;++i) { //stores the last occurence of the element i
  11.         C[i]+=C[i-1];
  12.     }
  13.     for(int i=n;i>=1;--i) {
  14.         B[C[A[i]]] = A[i]; //It will place the elements at their respective index
  15.         C[A[i]]--; //decrease count for same numbers
  16.     }
  17. }
  18. int main() {
  19.     int n;
  20.     cin>>n;
  21.     int A[n+1], B[n+1];
  22.     for(int i=1;i<=n;++i) {
  23.         cin>>A[i];
  24.         if(A[i]>mx) mx = A[i];
  25.     }
  26.     countSort(A, B, n);
  27.     for(int i=1;i<=n;++i) cout<<B[i]<<" ";
  28.     return 0;
  29. }
  30.  
  31. /*
  32. 10
  33. 12 345 89 100 23 0 18 44 111 1
  34. */
  35.  
  36. /*
  37. COUNTING_SORT (A, B, k)
  38.     1.  for i ← 1 to k do
  39.     2.      c[i] ← 0
  40.     3.  for j ← 1 to n do
  41.     4.      c[A[j]] ← c[A[j]] + 1
  42.     5.  // c[i] now contains the number of elements equal to i
  43.     6.  for i ← 2 to k do
  44.     7.      c[i] ← c[i] + c[i-1]
  45.     8.  // c[i] now contains the number of elements ≤ i
  46.     9.  for j ← n downto 1 do
  47.    10.      B[c[A[i]]] ← A[j]
  48.    11.      c[A[i]] ← c[A[j]] - 1
  49. */
  50.  
  51. /*
  52. Analysis
  53. The loop of lines 1-2   takes O(k) time
  54. The loop of lines 3-4   takes O(n) time
  55. The loop of lines 6-7   takes O(k) time
  56. The loop of lines 9-11 takes O(n) time
  57. Therefore, the overall time of the counting sort is O(k) + O(n) + O(k) + O(n) = O(k + n)
  58.  
  59.  
  60.  
  61. In practice, we usually use counting sort algorithm when have k = O(n), in which case running time is O(n).
  62.  
  63. The Counting sort is a stable sort i.e., multiple keys with the same value are placed in the sorted array in the same order that they appear in the input array.
  64.  
  65. Suppose that the for-loop in line 9 of the Counting sort is rewritten:
  66.  
  67. 9    for j ← 1 to n
  68.  
  69. then the stability no longer holds. Notice that the correctness of argument in the CLR does not depend on the order in which array A[1 . . n] is processed.
  70. The algorithm is correct no matter what order is used. In particular, the modified algorithm still places the elements with value k in position c[k - 1] + 1 through c[k], but in reverse order of their appearance in A[1 . . n].
  71.  
  72. Note that Counting sort beats the lower bound of  Ω(n lg n), because it is not a comparison sort. There is no comparison between elements. Counting sort uses the actual values of the elements to index into an array.
  73. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement