Question
Using Java/Eclipse, Write a class called SortComparator which contains a public static void main method. It should do the following: Creates an Integer array of
Using Java/Eclipse, Write a class called SortComparator which contains a public static void main method. It should do the following:
- Creates an Integer array of size 128. Call it a.
- Fills it up with random numbers between 0 and 100,000. Use
for (int k = 0; k < a.length; k++)
a[k] = (int) (Math.random() * 100000);
to generate your random numbers.
- Make 2 more copies of a, call them b and c. Dont just write b = a, etc. Create brand new arrays b and c of the same size as that of a and copy the elements of a into them using for-loops. (Note: as a result, all these arrays a, b, c finally contain exactly the same elements)
- Run the Selection sort algorithm on a and note the number of comparisons.
- Run the Insertion sort algorithm on b and note the number of comparisons.
- Run the Bubble sort algorithm on c and note the number of comparisons.
Repeat 1-6 above for sizes 512, 2048, and 8192. Based on the results you observe from these runs. Complete the following tables with results from your runs. Plot the results in Excel and include the plot in this document.
Number of Comparison Operations
Sorting Method | ArraySize =128 | ArraySize = 512 | ArraySize = 2048 | ArraySize = 8192 |
Selection Sort |
|
|
|
|
Insertion Sort |
|
|
|
|
Bubble Sort |
|
|
|
|
Which method had the least number of comparisons in each case? |
|
|
|
|
Run the same experiment again (Just run the program again. Results may be different since we are generating whole new sets of random arrays) and note down the results:
Sorting Method | ArraySize =128 | ArraySize = 512 | ArraySize = 2048 | ArraySize = 8192 |
Selection Sort |
|
|
|
|
Insertion Sort |
|
|
|
|
Bubble Sort |
|
|
|
|
Which method had the least number of comparisons in each case? |
|
|
|
|
SUBMISSION:
Copy and paste all the code and output into this file
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