Advertisement
Avatar_Fearless

Serching Program // Java

Mar 25th, 2012
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. import java.util.*;
  2. public class SearchingMain extends Dice
  3. {
  4. static final int NOT_FOUND = -1;
  5. static long bTime;
  6. static long lTime;
  7. static long start;
  8.  
  9. public SearchingMain()
  10. {
  11. bTime = 0;
  12. lTime = 0;
  13. start = 0;
  14. }
  15.  
  16. public static void main(String[] args)
  17. {
  18. int num1 = 1;
  19. final int CONSTANT = 1000;
  20. final int FIRST = 0;
  21. final int MIDDLE = 499;
  22. final int LAST = 999;
  23. final int DICESIZE = 1000000;
  24.  
  25. Dice roller = new Dice(DICESIZE);
  26. Integer[] theArray = new Integer[CONSTANT];
  27.  
  28. for(int k = 0; k < 1000; k++)
  29. {
  30. //int num = roller.roll();
  31. //theArray[k] = num;
  32. theArray[k] = num1;
  33. //System.out.println(num1);
  34. num1++;
  35. }
  36.  
  37. //System.out.println("First Value: " + theArray[FIRST] + " has an index of " + FIRST);
  38. //System.out.println("Middle Value: " + theArray[MIDDLE] + " has an index of " + MIDDLE);
  39. //System.out.println("Last Value: " + theArray[LAST] + " has an index of " + LAST);
  40.  
  41. System.out.println("Searching for first value... " + theArray[FIRST] + ", the first value is stored at index location " + linearSearch(theArray, new Integer(theArray[FIRST])));
  42. System.out.println("Searching for middle value... " + theArray[(CONSTANT / 2) - 1] + ", the middle value is stored at index location " + linearSearch(theArray, new Integer(theArray[(CONSTANT / 2) - 1])));
  43. System.out.println("Searching for last value... " + theArray[CONSTANT - 1] + ", the first last is stored at index location " + linearSearch(theArray, new Integer(theArray[CONSTANT - 1])));
  44. System.out.println("Searching for value -1... returns " + linearSearch(theArray, new Integer(-1)));
  45. System.out.println("Searching for value 100001... returns " + linearSearch(theArray, new Integer(100001)));
  46. System.out.println("The linear search took " + lTime + " milliseconds.");
  47.  
  48. System.out.println("Searching for first value... " + theArray[FIRST] + ", the first value is stored at index location " + binarySearch(theArray, new Integer(theArray[FIRST])));
  49. System.out.println("Searching for middle value... " + theArray[(CONSTANT / 2) - 1] + ", the middle value is stored at index location " + binarySearch(theArray, new Integer(theArray[(CONSTANT / 2) - 1])));
  50. System.out.println("Searching for last value... " + theArray[CONSTANT - 1] + ", the first last is stored at index location " + binarySearch(theArray, new Integer(theArray[CONSTANT - 1])));
  51. System.out.println("Searching for value -1... returns " + binarySearch(theArray, new Integer(-1)));
  52. System.out.println("Searching for value 100001... returns " + binarySearch(theArray, new Integer(100001)));
  53. System.out.println("The binary search took " + bTime + " milliseconds.");
  54.  
  55. }
  56. private static int linearSearch(Object[] list, Object x)
  57. {
  58. start = System.currentTimeMillis();
  59.  
  60. for(int k = 0; k < list.length; k++)
  61. {
  62. if(list[k].equals(x))
  63. return k;
  64. }
  65. lTime = System.currentTimeMillis() - start;
  66. return NOT_FOUND;
  67. }
  68.  
  69. private static int binarySearch(Comparable[] list, Comparable x)
  70. {
  71. start = System.currentTimeMillis();
  72. int low = 0;
  73. int high = list.length - 1;
  74. int mid;
  75.  
  76.  
  77. while (low <= high)
  78. {
  79.  
  80. mid = (low + high) / 2;
  81. int check = list[mid].compareTo(x);
  82. if (check == 0)
  83. {
  84. bTime = System.currentTimeMillis() - start;
  85. return mid;
  86. }
  87.  
  88.  
  89. if (check < 0)
  90. low = mid + 1;
  91. else
  92. high = mid - 1;
  93. }
  94. bTime = System.currentTimeMillis() - start;
  95. return NOT_FOUND;
  96.  
  97. }
  98.  
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement