Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. package hw1;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. /**
  7. * The Main class primarily runs all the instructions needed to run the program.
  8. * It does that by using functions and methods from other classes.
  9. *
  10. * @author Philip Bei Xu Yong
  11. */
  12. public class Main
  13. {
  14. /**
  15. * Contains everything needed to run the program by referencing other classes for functions and methods.
  16. * @param args System arguments
  17. * @throws IOException Input/Output Exception
  18. */
  19. public static void main(String[]args) throws IOException
  20. {
  21. //Step 1
  22. //Read Text File
  23. File file = new File("items.txt");
  24. BufferedReader br = new BufferedReader(new FileReader(file));
  25. String st;
  26.  
  27. //Step 2
  28. Random r = new Random();
  29. int n = 10;
  30. System.out.println("n="+n);
  31. Node[][] bag = new Node[n][25];
  32.  
  33. int itemcount = 1;
  34.  
  35. //Create List
  36. itemsLinkedList ls;
  37. if((st = br.readLine())!=null)
  38. {
  39. ls = new itemsLinkedList(br.readLine().split(","));
  40. while((st = br.readLine())!=null)
  41. {
  42. ls.add(ls.firstNode, st.split(","));
  43. itemcount++;
  44. }
  45. //ls.print(ls.firstNode);
  46.  
  47. Node currentNode = ls.firstNode;
  48.  
  49. //Adding items to bag
  50. for(int x = 0; x < n; x++)
  51. {
  52. for(int y = 0; y < 25; y++)
  53. {
  54. currentNode.currentStrength = r.nextInt(currentNode.maxStrength - currentNode.minStrength) + currentNode.minStrength;
  55. bag[x][y] = currentNode;
  56. System.out.println(bag[x][y]);
  57. if(currentNode.next == null)
  58. {
  59. currentNode = ls.firstNode;
  60. }
  61. else
  62. {
  63. currentNode = currentNode.next;
  64. }
  65. }
  66. }
  67. System.out.println("Bags before sorting:");
  68. bagFunc.print(bag, n);
  69.  
  70. //Step 3
  71. //Selecting item to find
  72. System.out.println("Single search time: " + findObject.linearSearch(itemcount, ls, n, bag, true) + " nanoseconds.");
  73. long avgTime = 0;
  74. for(int x = 0; x < 10; x++)
  75. {
  76. avgTime += findObject.linearSearch(itemcount, ls, n, bag, false);
  77. }
  78. System.out.println("Average search time: " + avgTime + " nanoseconds.");
  79.  
  80. //Step 5
  81. //Sort the bag
  82. long start = System.nanoTime();
  83. bag = bagFunc.bagSort5(bag, n);
  84. System.out.println("Step 5: "+ (System.nanoTime() - start) + " nanoseconds.");
  85.  
  86. //Step 6
  87. start = System.nanoTime();
  88. bagFunc.bagSort6(bag, n);
  89. System.out.println("Step 6: "+ (System.nanoTime() - start) + " nanoseconds.");
  90.  
  91. System.out.println("Bags after sorting:");
  92. bagFunc.print(bag, n);
  93.  
  94. //Step 7
  95. System.out.println("Single search time: " + findObject.binarySearch(itemcount, ls, n, bag, true) + " nanoseconds.");
  96.  
  97. avgTime = 0;
  98. for(int x = 0; x < 10; x++)
  99. {
  100. avgTime += findObject.binarySearch(itemcount, ls, n, bag, false);
  101. }
  102. System.out.println("Average search time: " + (avgTime/10) + " nanoseconds.");
  103. }
  104. else
  105. {
  106. System.out.println("There's nothing in the file.");
  107. }
  108. br.close();
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement