Question
This assignment will explore the difference in run-times and complexities of search algorithms and sorting algorithms. Use theSystem.currentTimeMillis()function to measure the speed of your programs.
This assignment will explore the difference in run-times and complexities of search algorithms and sorting algorithms.
Use theSystem.currentTimeMillis()function to measure the speed of your programs. Use the program below as a guide.
import java.io.*; import java.text.*;
//Lets test how fast your computer can process your code! public class speedTest {
//main method public static void main(String[] args) throws IOException { //Some variables to help us out! long time, time2, finalTime; int i;
//Get the current time in milliseconds time = System.currentTimeMillis();
//Output time to the screen System.out.println("The Current Time is: " + time);
//Here is some code for the computer to run. //You may have to change 500 to a higher number //if your computer is really really fast for(i = 0; i < 500; i++) { System.out.print("."); }
//Get the time at the end of the program time2 = System.currentTimeMillis();
//Output the end time and the difference in times! System.out.println(""); System.out.println("The End Time is: "+time2);
finalTime = time2 - time; System.out.println("It took " +finalTime+ " Milliseconds"); }
}
I need the following tasks:
- need to Compare sorting algorithms: bubble sort and insertion sort.
- need to Write two methods that populate an array- one that populates the array in reverse order, the other that populates the array in a nearly sorted fashion.
- need to Write the bubble sort method. This method must produce output that states the total run-time of the algorithm, as in the speedTest.java example.
- need to Write the insertion sort method. This method must produce output that states the total run-time of the algorithm, as in the speedTest.java example.
- Initialize an integer array using the methods from (a) and run each sort on the unsorted array. Compare the run times between the two different array arrangements and the two different sorting methods.
- In Big-O notation, compare the worst-case complexities of the two algorithms.
- Which is more efficient? Which criteria must be taken into account when choosing a sort algorithm to use the most efficient?
- need to Compare search algorithms: sequential search and binary search.
- Declare and initialize two arrays: one sorted and the other unsorted.
- Write the sequential search method. This method must produce output that states the total run-time, as in the speedTest.java example.
- Write the binary search method. This method must produce output that states the total run-time, as in the speedTest.java example.
- Run the sequential search method and the binary search method on the sorted array. Compare the run times of the two.
- Run the sequential search method on the unsorted array. Then call a sort method from (1) to sort the array. Next, call binary search on the sorted array. Compare the run times of the sequential search vs sorting + binary search.
- In Big-O notation, compare the worst-case complexities of the two algorithms.
- Which search method is more efficient? Is this true for all cases?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started