Question
Need help in java: Write a NetBeans project that compares the running times for the following array sorting algorithms: Simple Bubble Sort Based on two
Need help in java:
Write a NetBeans project that compares the running times for the following array sorting algorithms:
Simple Bubble Sort
Based on two nested loops
Does not check to see if it can stop early
Enhanced Bubble Sort
Based on two nested loops
Reduces the length traversed by the inner loop by one on each outer loop pass to reflect the fact that the largest(smallest) number moves to the bottom of the unsorted segment of the array on each pass
Stops if it ever makes an inner loop pass without making a swap, this means the array is in sorted order
Insertion Sort
Based on the insertion sort algorithm covered in CSCI 160
See code at end of assignment
Selection Sort
Based on the selection sort algorithm covered in CSCI 160
See code at end of assignment
For each of the above sorts:
Create an array of random integers
Sort the array
Display
The time that it took to sort the array
The first 5 items in the sorted array
See sample output at end of the assignment
Run multiple times were the size of the array varies from
10 to 100,000 in multiples of 10, e.g.
10 100 1,000 10,000 100,000
public static void selectionSort( int [ ] array )
{
int temp; // temporary location for swap
int max; // index of maximum value in subarray
for ( int i = 0; i < array.length; i++ )
{
// find index of largest value in subarray
max = indexOfLargestElement( array, array.length - i );
// swap array[max] and array[array.length - i - 1]
temp = array[max];
array[max] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
}
/** Finds index of largest element
* @param size the size of the subarray
* @param array the array to search
* @ return the index of the largest element in the subarray
*/
private static int indexOfLargestElement( int [ ] array, int size )
{
int index = 0;
for( int i = 1; i < size; i++ )
{
if ( array[i] > array[index] )
index = i;
}
return index;
}
/** Insertion Sort
* Anderson, Franceschi
* Java Illuminated, Chapter 8
* @param array array to sort
*/
public static void insertionSort( int [ ] array )
{
int j, temp;
for ( int i = 1; i < array.length; i++ )
{
j = i;
temp = array[i];
while ( j != 0 && array[j - 1] > temp )
{
array[j] = array[j - 1];
j--;
}
array[j] = temp;
}
}
Example Output:
Simple Bubble Sort N = 10 time = 0 msec head = 56985 90921 132061 289858 313708
Simple Bubble Sort N = 100 time = 0 msec head = 5180 12264 17859 40316 57085
Simple Bubble Sort N = 1,000 time = 0 msec head = 745 1522 1994 2487 2669
Simple Bubble Sort N = 10,000 time = 172 msec head = 4 13 34 204 209
Simple Bubble Sort N = 100,000 time = 13,922 msec head = 6 7 19 46 56
Enhanced Bubble Sort N = 10 time = 0 msec head = 9984 309503 600620 602265 611182
Enhanced Bubble Sort N = 100 time = 0 msec head = 1854 16291 16602 18077 24435
Enhanced Bubble Sort N = 1,000 time = 0 msec head = 812 1418 1529 1889 4388
Enhanced Bubble Sort N = 10,000 time = 125 msec head = 77 127 207 318 336
Enhanced Bubble Sort N = 100,000 time = 13,875 msec head = 32 33 33 51 57
Insertion Sort N = 10 time = 0 msec head = 19639 32970 39764 266798 266890
Insertion Sort N = 100 time = 0 msec head = 2152 43975 47944 50986 56473
Insertion Sort N = 1,000 time = 0 msec head = 774 2114 2999 3021 3259
Insertion Sort N = 10,000 time = 0 msec head = 15 602 736 1036 1132
Insertion Sort N = 100,000 time = 828 msec head = 4 10 15 35 45
Selection Sort N = 10 time = 0 msec head = 47350 371078 373696 376427 395348
Selection Sort N = 100 time = 0 msec head = 2864 11524 13169 19813 27526
Selection Sort N = 1,000 time = 0 msec head = 1241 1803 3809 6455 6996
Selection Sort N = 10,000 time = 31 msec head = 146 286 318 496 780
Selection Sort N = 100,000 time = 2,391 msec head = 2 7 23 34 37
BUILD SUCCESSFUL (total time: 31 seconds)
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