Advertisement
claukiller

Untitled

Feb 10th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. package labs.course15_16.lab1_2;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Loop4 {
  6. public static void loop3(int n) {
  7. /*Quadratic algorithm O(n^2) */
  8. Random rn = new Random();
  9. @SuppressWarnings("unused")
  10. int cont=0;
  11. for (int i=1; i<=n; i++)
  12. for (int j=1; j<=i; j++)
  13. for (int k=1; k<=j; k++)
  14. for (int l=1; l<=k; l++)
  15. cont += rn.nextInt();
  16. }
  17.  
  18. public static void main(String arg []){
  19. long t1,t2;
  20. int nTimes= Integer.parseInt(arg [0]);
  21.  
  22. for (int n=1; n<=100000; n*=2) {
  23. t1 = System.currentTimeMillis();
  24.  
  25. for (int repetitions=1; repetitions<=nTimes; repetitions++) {
  26. loop3(n);
  27. }
  28.  
  29. t2 = System.currentTimeMillis();
  30. System.out.println ("n="+n+ "**TIME="+(t2-t1)+" **nTimes="+nTimes);
  31. } // for
  32.  
  33. } // main
  34. } //class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement