Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. /**
  4. * This class looks like it's meant to provide a few public static methods
  5. * for searching and sorting arrays. It also has a main method that tests
  6. * the searching and sorting methods.
  7. *
  8. * TODO: The search and sort methods in this class contain bugs that can
  9. * cause incorrect output or infinite loops. Use the Eclipse debugger to
  10. * find the bugs and fix them
  11. */
  12. public class BuggySearchAndSort {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. int[] A = new int[10]; // Create an array and fill it with small random ints.
  17. for (int i = 0; i < 10; i++)
  18. A[i] = 1 + (int)(10 * Math.random());
  19.  
  20. int[] B = A.clone(); // Make copies of the array.
  21. int[] C = A.clone();
  22. int[] D = A.clone();
  23.  
  24. System.out.print("The array is:");
  25. printArray(A);
  26.  
  27. if (contains(A,5))
  28. System.out.println("This array DOES contain 5.");
  29. else
  30. System.out.println("This array DOES NOT contain 5.");
  31.  
  32. Arrays.sort(A); // Sort using Java's built-in sort method!
  33. System.out.print("Sorted by Arrays.sort(): ");
  34. printArray(A); // (Prints a correctly sorted array.)
  35.  
  36. bubbleSort(B);
  37. System.out.print("Sorted by Bubble Sort: ");
  38. printArray(B);
  39.  
  40. selectionSort(C);
  41. System.out.print("Sorted by Selection Sort: ");
  42. printArray(C);
  43.  
  44. insertionSort(D);
  45. System.out.print("Sorted by Insertion Sort: ");
  46. printArray(D);
  47.  
  48. }
  49.  
  50. /**
  51. * Tests whether an array of ints contains a given value.
  52. * @param array a non-null array that is to be searched
  53. * @param val the value for which the method will search
  54. * @return true if val is one of the items in the array, false if not
  55. */
  56. public static boolean contains(int[] array, int val) {
  57. for (int i = 0; i < array.length; i++) {
  58. if (array[i] == val)
  59. return true;
  60.  
  61. }
  62. return false;
  63. }
  64.  
  65. /**
  66. * Sorts an array into non-decreasing order. This inefficient sorting
  67. * method simply sweeps through the array, exchanging neighboring elements
  68. * that are out of order. The number of times that it does this is equal
  69. * to the length of the array.
  70. */
  71. public static void bubbleSort(int[] array) {
  72. for (int i = 0; i < array.length; i++) {
  73. for (int j = 0; j < array.length-1; j++) {
  74. if (array[j] > array[j+1]) { // swap elements j and j+1
  75. int temp = array[j];
  76. array[j] = array[j+1];
  77. array[j+1] = temp;
  78. }
  79. }
  80. }
  81. }
  82.  
  83. /**
  84. * Sorts an array into non-decreasing order. This method uses a selection
  85. * sort algorithm, in which the largest item is found and placed at the end of
  86. * the list, then the second-largest in the next to last place, and so on.
  87. */
  88. public static void selectionSort(int[] array) {
  89. for (int top = array.length - 1; top > 0; top--) {
  90. int positionOfMax = 0;
  91. for (int i = 0; i <= top; i++) {
  92. if (array[i] > array[positionOfMax])
  93. positionOfMax = i;
  94. }
  95. int temp = array[top]; // swap top item with biggest item
  96. array[top] = array[positionOfMax];
  97. array[positionOfMax] = temp;
  98. }
  99. }
  100.  
  101. /**
  102. * Sorts an array into non-decreasing order. This method uses a standard
  103. * insertion sort algorithm, in which each element in turn is moved downwards
  104. * past any elements that are greater than it.
  105. */
  106. public static void insertionSort(int[] array) {
  107. for (int top = 1; top < array.length; top++) {
  108. int temp = array[top]; // copy item that into temp variable
  109. int pos = top ;
  110. while (pos > 0 && array[pos-1] >= temp) {
  111. // move items that are bigger than temp up one position
  112. array[pos] = array[pos-1];
  113. pos--;
  114. }
  115. array[pos] = temp; // place temp into last vacated position
  116. }
  117. }
  118.  
  119. /**
  120. * Outputs the ints in an array on one line, separated by spaces,
  121. * with a line feed at the end.
  122. */
  123. private static void printArray(int[] array) {
  124. for (int i = 0; i < array.length; i++) {
  125. System.out.print(" ");
  126. System.out.print(array[i]);
  127. }
  128. System.out.println();
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement