Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a package called ceQuick Add a class called SortComparison that includes the main method. Create a private static method called getRandomNumberArray . It returns

  • Create a package called ceQuick
  • Add a class called SortComparison that includes the main method.
  • Create a private static method called getRandomNumberArray. It returns an array of Integer and has 2 parameters: arraySize of type int and numberOfDigits of type int. This method should create an array of the specified size that is filled with random numbers where each random number has the number of digits specified in the second parameter (no leading zeros). Write some test code to verify that the method getRandomNumberArray works as expected. Once you are confident that this is the case, remove the test code. Because of the limited range of integer values, the number of digits needs to be a value from the range [1,10]. If the second argument is not within this range, throw an IllegalArgumentException that includes the message: "The number of digits needs to be a value between 1 and 10."
  • In your main method (or additional private methods) do the following: Repeatedly execute selection sort and quicksort on an array of numbers
    • Create an array of n 7-digit numbers, where n is 1000. If the performance of your computer significantly differs from the sample output, you might need to start with a smaller or larger n.
    • To ensure a meaningful comparison, both algorithms need to sort the exact same sequence of random numbers (e.g. if I run selection sort on [1, 5, 2, 7, 6, 4, 9, 8] then I need to run quicksort on [1, 5, 2, 7, 6, 4, 9, 8]. Create a copy of the array to ensure that both sorting methods are called on the identical array.
    • Use the method nanoTime from class System to measure how long it takes to sort with Selection sort and how long it takes to sort with quicksort.
    • Print the results side-by-side in tabular form as shown in the sample output
    • After the two sorting algorithms have been executed and the elapsed time has been printed, the size of n is doubled so that in the next iteration a new array of twice the size can be created.
    • Print the output in tabular form as shown in the output. Notice the header line, straight columns, and dashes and vertical bars as dividers. The duration should be displayed in seconds and it should list 4 digits after the decimal point.
    The table should have eight to ten rows, and the size of n needs to be chosen to show the following patterns that indicate the big O of the algorithm.
    • If the duration after doubling the size of n is approximately 4 times bigger than before, that indicates O(N2)
    • If the duration after doubling the size of n is about twice as big as before, that indicates big O(N)
    • If the duration after doubling the size of n is a bit more than twice as big as before, that indicates big O(N log N)

Sample Output

The actual numbers will differ. However, the importance is the demonstration of the following two patterns: That the measured time of Selection sort approximately quadruples as n is doubled and the measured time of Quicksort slightly more than doubles. The measurements are less precise when n is small, so run the sorting algorithms often enough to display decisive results. Depending on your computer, you might need to adjust n by starting with a smaller or larger number.

image text in transcribed

n | Selection Quick -T- - 1,000 0.01915 1 0.00885 1 2,000 0.0471s 1 0.0011s 1 4,000 0.0660s | 0.0045s ] 8,000 0.08605 0.0116s 16,000 0.30635 1 0.05385 32,000 1.44005 1 0.0202 s 64,000 5.5242s 1 0.0346s 128,000 | 42.9131s 1 0.07365 256,000 | 154.23905 | 0.15105 512,000 690.5412s 0.3169 1 n | Selection Quick -T- - 1,000 0.01915 1 0.00885 1 2,000 0.0471s 1 0.0011s 1 4,000 0.0660s | 0.0045s ] 8,000 0.08605 0.0116s 16,000 0.30635 1 0.05385 32,000 1.44005 1 0.0202 s 64,000 5.5242s 1 0.0346s 128,000 | 42.9131s 1 0.07365 256,000 | 154.23905 | 0.15105 512,000 690.5412s 0.3169 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions