Advertisement
claukiller

Untitled

Feb 17th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. package labs.course15_16.lab2;
  2.  
  3. /* This program can be used to sort n elements with
  4. * a "bad" algorithm (quadratic).
  5. * It is the DIRECT INSERTION */
  6. public class Insertion1{
  7. static int []v;
  8.  
  9. public static void main (String arg [] ){
  10. int nTimes= Integer.parseInt (arg[0]); //size of the problem
  11. long t1,t2;
  12. v = new int [nTimes];
  13. int counter =0;
  14. for (int i = 10000; i <=1280000;i*=2){
  15. v = new int [i];
  16. //Vector.inverselySorted(v);
  17. for (int repetition=1; repetition<=nTimes; repetition++){
  18.  
  19. Vector.inverselySorted(v);
  20. t1=System.currentTimeMillis();
  21. insertion(v);
  22. t2=System.currentTimeMillis();
  23. counter+= t2-t1;
  24. }
  25.  
  26. System.out.println("n: "+i+" "+"TIME: " +(t2-t1));
  27. }
  28.  
  29. }
  30.  
  31. public static void insertion(int[] elements) {
  32. int j;
  33. int pivot;
  34.  
  35. for (int i = 1; i < elements.length; i++) {
  36. pivot = elements[i];
  37. j = i-1;
  38.  
  39. while (j >= 0 && pivot < elements[j]) {
  40. elements[j+1] = elements[j];
  41. j--;
  42. }
  43.  
  44. elements[j+1] = pivot;
  45. }
  46.  
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement