Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. /*
  2. * Author Farhan Khwaja ( www.code2learn.com )
  3. *
  4. */
  5. public class CountingSort {
  6.  
  7. private static int[] c;
  8.  
  9. public void CountSort(int[] a, int[]b ,int k){
  10. c=new int[k+1];
  11. for(int i=0;i<=k;i++){
  12. c[i]=0;
  13. }
  14. for(int i=0;i<a.length;i++){
  15. c[a[i]]=c[a[i]]+1;
  16. }
  17. for(int i=1;i<=k;i++){
  18. c[i]=c[i]+c[i-1];
  19. }
  20. for(int i=a.length-1;i>=0;i--){
  21. b[c[a[i]]-1]=a[i];
  22. c[a[i]]=c[a[i]]-1;
  23. }
  24. }
  25.  
  26. public static void main(String[] args) {
  27. long start_time = System.nanoTime();
  28. numbergenerator v = new numbergenerator();
  29.  
  30. for (int j = 0; j < 1000; j++){
  31.  
  32. int[] l= v.randomarray(j);
  33. int[] k=new int[l.length];
  34. new CountingSort().CountSort(l, k, 100);
  35.  
  36. long ex_time = System.nanoTime();
  37. long nano = ex_time - start_time;
  38. System.out.println(nano);
  39.  
  40. }
  41.  
  42.  
  43.  
  44.  
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement