question about counting sort

Posted by davit-datuashvili on Stack Overflow See other posts from Stack Overflow or by davit-datuashvili
Published on 2010-05-30T15:49:49Z Indexed on 2010/05/30 15:52 UTC
Read the original article Hit count: 221

Filed under:

hi i have write following code which prints elements in sorted order only one big problem is that it use two additional array here is my code

public  class occurance{
public static   final  int n=5;


public static void main(String[]args){
// n  is  maximum possible  value  what it should be in array suppose n=5 then array may be

int  a[]=new int[]{3,4,4,2,1,3,5};// as   u see all elements are less or equal to n
//create array a.length*n

int b[]=new int[a.length*n];
int c[]=new int[b.length];
 for (int i=0;i<b.length;i++){
  b[i]=0;
  c[i]=0;
}

  for (int i=0;i<a.length;i++){

   if (b[a[i]]==1){
  c[a[i]]=1;
}
 else{
 b[a[i]]=1;
}
}
 for (int i=0;i<b.length;i++){
   if (b[i]==1) {
   System.out.println(i);
}
  if (c[i]==1){
  System.out.println(i);
}
}





}
}
//
1
2
3
3
4
4
5

1.i have two question what is complexity of this algorithm?i mean running time 2. how put this elements into other array with sorted order? thanks

© Stack Overflow or respective owner

Related posts about algorithm