Programming sorting algorithms

Assignment Help JAVA Programming
Reference no: EM13709852

Programming Sorting Algorithms:

a. Describe an approach to modifying the Sorts.java program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method.

b. Implement your approach.

c. Test your new program by running the selectionSort method.

Your program should report 49 swaps.

d. Now, modify your program to also output the number of comparisons (compares) needed.

You must include one or more statements to increment your counter within the sorting methods themselves.

For each of the listed methods, make and test the changes needed, and list both the number of swaps and the number of compares needed by the Sorts program to sort an array of 50 random integers.

selectionSort swaps:____

compares:____

bubbleSort swaps:____

compares:____

shortBubble swaps:____

compares:____

insertionSort swaps:____

compares:____

//---------------------------------------------------------------------------- //Sorts.java //

// Test harness used to run sorting algorithms.

//---------------------------------------------------------------------------- import java.util.*; import java.text.DecimalFormat; public class Sorts

{

static final int SIZE = 50;

// size of array to be sorted static int[] values = new int[SIZE];

// values to be sorted static void initValues()

// Initializes the values array with random integers from 0 to 99.

{ Random rand = new Random(); for (int index = 0; index < SIZE; index++) values[index] = Math.abs(rand.nextInt()) % 100; }

static public boolean isSorted() // Returns true if the array values are sorted and false otherwise.

{ boolean sorted = true; for (int index = 0; index < (SIZE - 1); index++) if (values[index] > values[index + 1]) sorted = false; return sorted; }

Reference no: EM13709852

Questions Cloud

How fast is each one going, when they are far apart : Two positive point charges, qa and qb (masses ma and mb) are at rest, half together by mass less string of length a. How fast is each one going, when they are far apart
Find the work done during the expansion : A piston cylinder with a set of stops contains 10 kg of R-134a refrigerant. Initially, 8kg of the R-134a is liquid and the rest a gas. At -8C. Find the work done during the expansion
Explain the process to finding information for research : We are often asked about things we don't have knowledge of, let's say you have been asked to determine how hospitals prepare and train volunteers.
Determine the magnitude of the lift force : A jet airplane is in level flight. The mass of the airplane is m = 8850 kg. What is the magnitude of the lift force acting on the plane
Programming sorting algorithms : Describe an approach to modifying the Sorts.java program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method.
What is its closest approach to the surface of the nucleus : A proton is fired from far away toward the nucleus of a mercury atom. Mercury is element number 80, and the diameter of the nucleus is 14.0 fm. what is its closest approach to the surface of the nucleus
Explain the effective warehouse design : Why is it important for businesses to utilize their warehouse space effectively? Be sure to define "effective" and make a good argument for your position.
How much work is done by the object in sliding : A 2.0-kg object is at rest at the top of an inclined plane that makes an angle of 30° with the horizontal and which has a rise of 0.40 m. How much work is done by the object in sliding
How many grams of sucrose would have to be added : How many grams of sucrose would have to be added to 0.250 kg of water to elevate the boiling point by 5.0 Celcius

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write a class named java1306cmis141c801 that performs the

write a class named java1306cmis141c801 that performs the following actions.prompt the user for an int between lower

  Writing a simple gui application using a class called myguic

writing a simple GUI application using a class called MyGuiClass. Your GUI will have a JButton which your program will need to respond to when it is clicked. Describe what you would need to do to setup event handling using a nested inner class. Use J..

  File integrity checker - tripwire

Write a program that will perform some of the basic tasks accomplished by a file integrity checker such as Tripwire.

  Write java program to select pine for furniture company

Write down the Java program for the furniture company. Ask user to select P for Pine, O for Oak or M for Mahogany.

  Compare and contrast the treatment of war

Compare and contrast the treatment of war in O'Brien's "The Things They Carried" and Frank O'Connor's "Guest of the Nation." 3-5 page MLA Format

  Write an application bmicalc

Write an application (BMICalc) that reads the user's weight in poinds and height in inches, then calculates the Body Mass Index.

  Implement/update specific methods for the dfs of a graph

show the DFS order of vertices in the graph, and for each node, specify its parent node in the search (the node from which the currect node was reached). Moreover, display for each node the discovery and finishing time, to check that the Parenthesis ..

  Java class to accept a user-s hourly rate of pay

Write a class that accepts a user's hourly rate of pay and the number of hours worked. Display the user's gross pay, the withholding tax (15% of gross pay), and the net pay (gross pay - withholding).

  Assignmentwrite a gui-based application that allows a user

assignmentwrite a gui-based application that allows a user to play a simple trivia game. the game should have 5

  Design and implement an applet called circles

Design and implement an applet called Circles that draws 50 circles of random diameter in random locations. If the diameter of a circle is less than a certain value, the circle is ?lled with the color yellow.

  Implement a shopping cart class with user interface

project will be to implement a shopping cart class with user interface (UI) that contains main() in Net Beans. The UI class will be used to perform user input/output and to invoke the appropriate methods of shopping cart class. When your program star..

  Write a class that implements an ordered list of strings

In this problem you will write a class that implements an ordered list of Strings.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd