Skip to main content

[SOLVED] Buggy Search And Sort Buy Reporting with Java source code

Report The Bug From The Following Source Code:

import java.util.Arrays;
/**
* This class looks like it's meant to provide a few public static methods for
* searching and sorting arrays. It also has a main method that tests the
* searching and sorting methods.

* TODO: The search and sort methods in this class contain bugs that can cause
* incorrect output or infinite loops. Use the Eclipse debugger to find the bugs
* and fix them
*/

public class BuggySearchAndSort {

public static void main(String[] args) {

int[] A = new int[10]; // Create an array and fill it with random ints.
for (int i = 0; i < 10; i++) {
A[i] = 1 + (int) (10 * Math.random());
}

int[] B = A.clone(); // Make copies of the array.
int[] C = A.clone();
int[] D = A.clone();

System.out.print("The array is:");
printArray(A);

if (contains(A, 5)) {
System.out.println("This array DOES contain 5.");
} else {
System.out.println("This array DOES NOT contain 5.");
}

Arrays.sort(A); // sort using Java's built-in sort method!
System.out.print("Sorted by Arrays.sort(): ");
printArray(A);

sweepSort(B);
System.out.print("Sorted by Sweep Sort: ");
printArray(B);

selectionSort(C);
System.out.print("Sorted by Selection Sort: ");
printArray(C);

insertionSort(D);
System.out.print("Sorted by Insertion Sort: ");
printArray(D);

}

/**
* Tests whether an array of ints contains a given value.

* @param array
* a non-null array that is to be searched
* @param val
* the value for which the method will search
* @return true if val is one of the items in the array, false if not
*/
public static boolean contains(int[] array, int val) {
for (int i = 0; i < array.length; i++) {
if (array[i] == val) {
return true;
} else {
return false;
}
}
return false;
}

/**
* Sorts an array into non-decreasing order. This inefficient sorting method
* simply sweeps through the array, exchanging neighboring elements that are
* out of order. The number of times that it does this is equal to the
* length of the array.
*/
public static void sweepSort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] array[j + 1]) { // swap elements j and j+1
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}

/**
* Sorts an array into non-decreasing order. This method uses a selection
* sort algorithm, in which the largest item is found and placed at the end
* of the list, then the second-largest in the next to last place, and so
* on.
*/
public static void selectionSort(int[] array) {
for (int top = array.length - 1; top 0; top--) {
int positionOfMax = 0;
for (int i = 1; i <= top; i++) {
if (array[i] array[positionOfMax]) {
positionOfMax = i;
}
}
int temp = array[top]; // swap top item with biggest item
array[top] = array[positionOfMax];
array[positionOfMax] = temp;
}
}

/**
* Sorts an array into non-decreasing order. This method uses a standard
* insertion sort algorithm, in which each element in turn is moved
* downwards past any elements that are greater than it.
*/
public static void insertionSort(int[] array) {
for (int top = 1; top < array.length; top++) {
int pos =top;
while (pos 0) {
if(array[pos] < array[pos-1]){
int temp = array[pos-1];
array[pos - 1] = array[pos];
array[pos] = temp;
}
pos--;
}
}
}

/**
* Outputs the ints in an array on one line, separated by spaces, with a
* line feed at the end.
*/
private static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(" ");
System.out.print(array[i]);
}
System.out.println();
}

}

Get your solution now 

Buy now

Comments

Popular posts from this blog

The Zoo Management System - entity relationship diagram & MS Access Database

Zoo Management System - Project Details: You are the employee of a big, worldwide working Zoo Management Company. Your company is responsible for the Zoo management. Your boss thinks it would be a great idea to store all data for each Zoo in a brand new self-developed ZOO Management System. Up to now, the ZOO management company has maps of each ZOO available. Your boss knows that you took a course in introduction on an ERP system, so he asks you if you could help designing such a system. Each ZOO must have the same organizational structure, which should look like this: Each Zoo has a Zoo-Address. Each Zoo has many visitors (Visitor Ticket Process (VTP). Many Zoo-Attractions belong to a Zoo. Module 1: Entity Relationship Diagram Design a ER (entity-relationship) diagram for your ZOO Management System. Use the information provided below with the entities and its attributes. Put the entities in the correct relationship to each other (organizational structure). Module 2: DB Implem...

Human Physiology by Stuart Ira Fox [PDF] (12th edition) free download

Sudoku Game Project in Java with full source code including GUI task

Introduction The purpose of the project is to try in practice the use of recursion and object-oriented program design. Please make sure to read this entire note before starting your work. Pay close attention to the sections on deadlines, deliverables, and exam rules. The problem Your task in this part of the project is to write a solver for Sudoku puzzles. Sudoku puzzles are a kind of crossword puzzles with numbers where the following two conditions have to be met: In each row or column, the nine numbers have to be from the set [1,2,3,4,5,6,7,8,9] and they must all be different.  For each of the nine non-overlapping 3x3 blocks, the nine numbers have to be from the set [1,2,3,4,5,6,7,8,9] and they ust all be different. The following two figures show a Sudoku puzzle and its solution. The input The input of your program, the Sudoku puzzles, is given in text files. More specifically, they are represented as nine lines of nine characters separated by spaces. The characters can be of two...