Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. import java.io.*;
  4.  
  5. public class TestSort {
  6. public static void main(String[] args) throws IOException {
  7. //ArrayList
  8. //int N = (int)(Math.random() * 20000 + 1);
  9. for (int N = 100; N < 100000; N += 1000) {
  10. double[] data = new double[N];
  11. for (int i = 0; i < N; i++)
  12. data[i] = Math.random();
  13. double[] data1 = (double[])data.clone();
  14. double[] data2 = (double[])data.clone();
  15. double[] data3 = (double[])data.clone();
  16. BufferedWriter writer = new BufferedWriter(new FileWriter("out.txt", true));
  17. long time_prev = System.nanoTime();
  18. InsertionSort(data1);
  19. double time = (System.nanoTime()-time_prev)/1000000000.0;
  20. System.out.println("Insertion Sort\nTime= " + time);
  21. writer.write(N + " ");
  22. writer.write(time + " ");
  23. time_prev = System.nanoTime();
  24. ShellSort(data2);
  25. time = (System.nanoTime()-time_prev)/1000000000.0;
  26. System.out.println("Shell Sort\nTime= " + time);
  27. writer.write(time + " ");
  28. time_prev = System.nanoTime();
  29. Arrays.sort(data3);
  30. time = (System.nanoTime()-time_prev)/1000000000.0;
  31. System.out.println("Quick Sort\nTime= " + time);
  32. writer.write(time + " ");
  33. writer.write("\n");
  34.  
  35. writer.close();
  36. System.out.println("\tPresorted\tInsertion\t\t Shell\t\t Quick");
  37. for (int i=0; i<data.length; i++)
  38. System.out.println(data[i] + " " + data1[i] + " " + data2[i] + " " + data3[i]);
  39. }
  40.  
  41. }
  42.  
  43.  
  44.  
  45.  
  46. // I changed this as the previous code was terribly inefficient
  47.  
  48. public static void InsertionSort(double[] data) {
  49. for (int i = 1; i < data.length; i++) {
  50. if (data[i]<data[i-1]) {
  51. double temp = data[i];
  52. int j = i;
  53. do {
  54. data[j] = data[j-1];
  55. j--;
  56. } while (j>0 && data[j-1] > temp);
  57. data[j] = temp;
  58. }
  59. }
  60. }
  61.  
  62. public static void ShellSort(double[] a) {
  63. int increment = a.length / 3 + 1;
  64.  
  65. // Sort by insertion sort at diminishing increments.
  66. while ( increment > 1 )
  67. {
  68. for ( int start = 0; start < increment; start++ )
  69. insertSort(a, start, increment );
  70.  
  71. increment = increment / 3 + 1;
  72. }
  73.  
  74. // Do a final pass with an increment of 1.
  75. // (This has to be outside the previous loop because
  76. // the formula above for calculating the next
  77. // increment will keep generating 1 repeatedly.)
  78. insertSort(a, 0, 1 );
  79. }
  80. public static void insertSort(double[] a, int start, int increment ) {
  81. int j, k;
  82. double temp;
  83.  
  84. for ( int i = start + increment; i < a.length; i += increment )
  85. {
  86. j = i;
  87. k = j - increment;
  88. if ( a[j] < a[k] )
  89. {
  90. // Shift all previous entries down by the current
  91. // increment until the proper place is found.
  92. temp = a[j];
  93. do
  94. {
  95. a[j] = a[k];
  96. j = k;
  97. k = j - increment;
  98. } while ( j != start && a[k] > temp );
  99. a[j] = temp;
  100. }
  101. }
  102. }
  103. public static double median(double[] a) {
  104. Arrays.sort(a);
  105. return a[a.length/2];
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement