Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. public class Insertion {
  2.  
  3. public static void main(String[] args){
  4.  
  5. for(int i = 1000; i<=100000; i+=1000){
  6. initialize(i);
  7. }
  8. }
  9.  
  10. public static void initialize(int N){
  11. double[] data = new double[N];
  12. for (int i = 0; i < N; i++)
  13. data[i] = Math.random();
  14. double[] data1 = (double[])data.clone();
  15. long time_prev = System.nanoTime();
  16.  
  17. InsertionSort(data1);
  18. double time = (System.nanoTime()-time_prev)/1000000000.0;
  19. System.out.println(time);//Printing out time
  20.  
  21. }
  22.  
  23. public static void InsertionSort(double[] data) {
  24. for (int i = 1; i < data.length; i++) {
  25. if (data[i]<data[i-1]) {
  26. double temp = data[i];
  27. int j = i;
  28. do {
  29. data[j] = data[j-1];
  30. j--;
  31. } while (j>0 && data[j-1] > temp);
  32. data[j] = temp;
  33. }
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement