Advertisement
akashmallik

Counting Sort

Oct 5th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int input[100],output[100],temp[100],n,k;
  7.     cout<<"Length of input array: ";
  8.     cin>>n;
  9.     for(int i=1; i<=n; i++)
  10.     {
  11.         cin>>input[i];
  12.     }
  13.     k=input[1];
  14.     for(int i=2; i<=n; i++)
  15.     {
  16.         if(k<input[i])
  17.         {
  18.             k=input[i];
  19.         }
  20.     }
  21.     cout<<"Largest Number is"<<k<<endl;
  22.  
  23.     for(int i=1; i<=k; i++)
  24.     {
  25.         temp[i]=0;
  26.     }
  27.     cout<<"Declare temp array zero.."<<endl;
  28.     for(int i=1; i<=k; i++)
  29.     {
  30.         cout<<temp[i]<<endl;
  31.     }
  32.  
  33.  
  34.     for(int j=1; j<=n; j++)
  35.     {
  36.         temp[input[j]]+=1;
  37.     }
  38.     cout<<"Temp array.."<<endl;
  39.     for(int i=1; i<=k; i++)
  40.     {
  41.         cout<<temp[i]<<endl;
  42.     }
  43.  
  44.     for(int i=2; i<=k; i++)
  45.     {
  46.         temp[i]=temp[i]+temp[i-1];
  47.     }
  48.  
  49.     cout<<"Increment of temp array.."<<endl;
  50.     for(int i=1; i<=k; i++)
  51.     {
  52.         cout<<temp[i]<<endl;
  53.     }
  54.  
  55.     cout<<"Counting sort"<<endl;
  56.     for(int j=n; j>=1; j--)
  57.     {
  58.         output[temp[input[j]]]=input[j];
  59.         temp[input[j]]-=1;
  60.     }
  61.  
  62.     for(int i=1;i<=n;i++){
  63.         cout<<output[i]<<endl;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement