Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hw1;
- import java.io.*;
- import java.util.*;
- /**
- * The Main class primarily runs all the instructions needed to run the program.
- * It does that by using functions and methods from other classes.
- *
- * @author Philip Bei Xu Yong
- */
- public class Main
- {
- /**
- * Contains everything needed to run the program by referencing other classes for functions and methods.
- * @param args System arguments
- * @throws IOException Input/Output Exception
- */
- public static void main(String[]args) throws IOException
- {
- //Step 1
- //Read Text File
- File file = new File("items.txt");
- BufferedReader br = new BufferedReader(new FileReader(file));
- String st;
- //Step 2
- Random r = new Random();
- int n = 10;
- System.out.println("n="+n);
- Node[][] bag = new Node[n][25];
- int itemcount = 1;
- //Create List
- itemsLinkedList ls;
- if((st = br.readLine())!=null)
- {
- ls = new itemsLinkedList(br.readLine().split(","));
- while((st = br.readLine())!=null)
- {
- ls.add(ls.firstNode, st.split(","));
- itemcount++;
- }
- //ls.print(ls.firstNode);
- Node currentNode = ls.firstNode;
- //Adding items to bag
- for(int x = 0; x < n; x++)
- {
- for(int y = 0; y < 25; y++)
- {
- currentNode.currentStrength = r.nextInt(currentNode.maxStrength - currentNode.minStrength) + currentNode.minStrength;
- bag[x][y] = currentNode;
- System.out.println(bag[x][y]);
- if(currentNode.next == null)
- {
- currentNode = ls.firstNode;
- }
- else
- {
- currentNode = currentNode.next;
- }
- }
- }
- System.out.println("Bags before sorting:");
- bagFunc.print(bag, n);
- //Step 3
- //Selecting item to find
- System.out.println("Single search time: " + findObject.linearSearch(itemcount, ls, n, bag, true) + " nanoseconds.");
- long avgTime = 0;
- for(int x = 0; x < 10; x++)
- {
- avgTime += findObject.linearSearch(itemcount, ls, n, bag, false);
- }
- System.out.println("Average search time: " + avgTime + " nanoseconds.");
- //Step 5
- //Sort the bag
- long start = System.nanoTime();
- bag = bagFunc.bagSort5(bag, n);
- System.out.println("Step 5: "+ (System.nanoTime() - start) + " nanoseconds.");
- //Step 6
- start = System.nanoTime();
- bagFunc.bagSort6(bag, n);
- System.out.println("Step 6: "+ (System.nanoTime() - start) + " nanoseconds.");
- System.out.println("Bags after sorting:");
- bagFunc.print(bag, n);
- //Step 7
- System.out.println("Single search time: " + findObject.binarySearch(itemcount, ls, n, bag, true) + " nanoseconds.");
- avgTime = 0;
- for(int x = 0; x < 10; x++)
- {
- avgTime += findObject.binarySearch(itemcount, ls, n, bag, false);
- }
- System.out.println("Average search time: " + (avgTime/10) + " nanoseconds.");
- }
- else
- {
- System.out.println("There's nothing in the file.");
- }
- br.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement