Counting Sort

View previous topic View next topic Go down

Counting Sort

Post  skyboard on Thu Sep 15, 2011 3:58 pm

public class CountingSort{
public int[] sort(int[] a){
int[] c=new int[a.length];
for(int i=0;i<a.length;i++)
c[i]=0;
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]<a[j])
c[j]++;
else
c[i]++;
}
}
int[] b=new int[a.length];
for(int i=0;i<a.length;i++){
b[c[i]]=a[i];
}
return b;
}

public static void main(String[] args){
int[] key={1,4,6,2,3,7};
CountingSort count=new CountingSort();
int[] temp=count.sort(key);
String str=new String();
for(int i=0;i<key.length;i++){
str+=String.valueOf(temp[i]);
}
System.out.println(str);
}
}

skyboard

Posts : 31
Join date : 2011-09-03

View user profile

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum