Counting Sort
Page 1 of 1 • Share •
Counting Sort
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);
}
}
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

» Im back! Counting bites this time
» There's an iphone app for counting bites!!
» Alien bug thing again
» Mycorrhizae Product?
» Evolution Raceway Now using AMB/MYLAPS lap counter!!!!
» There's an iphone app for counting bites!!
» Alien bug thing again
» Mycorrhizae Product?
» Evolution Raceway Now using AMB/MYLAPS lap counter!!!!
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|