Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Author Farhan Khwaja ( www.code2learn.com )
- *
- */
- public class CountingSort {
- private static int[] c;
- public void CountSort(int[] a, int[]b ,int k){
- c=new int[k+1];
- for(int i=0;i<=k;i++){
- c[i]=0;
- }
- for(int i=0;i<a.length;i++){
- c[a[i]]=c[a[i]]+1;
- }
- for(int i=1;i<=k;i++){
- c[i]=c[i]+c[i-1];
- }
- for(int i=a.length-1;i>=0;i--){
- b[c[a[i]]-1]=a[i];
- c[a[i]]=c[a[i]]-1;
- }
- }
- public static void main(String[] args) {
- long start_time = System.nanoTime();
- numbergenerator v = new numbergenerator();
- for (int j = 0; j < 1000; j++){
- int[] l= v.randomarray(j);
- int[] k=new int[l.length];
- new CountingSort().CountSort(l, k, 100);
- long ex_time = System.nanoTime();
- long nano = ex_time - start_time;
- System.out.println(nano);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement