Question: I need help sorting my arrays with each sort algorithm and timing them, here is the assignment. Project Specification: Step 1: (Send the output to

I need help sorting my arrays with each sort algorithm and timing them, here is the assignment.

Project Specification:

Step 1: (Send the output to p6part1out.txt and have your name displayed on the first line.)

Generate random integers between 0 and the maximum value of Integer type to fill two arrays of size 10,000 each with the same value. Call these arrays array1 and array2.

Slightly modify the sample program ArraySorter (line 189) so that you can use them to sort array2.

Use nanoTime() to record the time it takes for each algorithm to sort array2.

To verify if you have used the sort program correctly, print the first 20 and the last 20 numbers of the sorted array.

Copy array1 to array2 before you try the different algorithm so that the same unsorted array will be used.

Repeat the above steps for 20 times.

Use Spreadsheet to record the time, find the average time for each algorithm and draw a line graph based upon Average time and the Sort algorithm.

Your program logic should be like the following:

For I from 1 to 20 do {

Generate 100,000 random integers to fill array1 and array2;

startTime = nanoTime();

Call Sort1 using array2;

elapseTime = nanoTime() startTime;

printFirstLast20(outfile1, array2);

outfile1.println(Sort1- + I + : Time = + elapseTime);

copyArray(array1, array2);

startTime = nanoTime();

Call Sort2 using array2;

elapseTime = nanoTime() startTime;

printFirstLast20(outfile2, array2);

outfile.println(Sort2- + I + : Time = + elapseTime);

(repeat the same process for every sort algorithm)

}

Finally, compile the above result and write your finding as you did Project 5. Call this file p6report.docx

Step 2: (Send the output to p6part2out.txt and have your name displayed on the first line.)

Apply step 1 to p1arts instead of integers. i.e., Read in p1arts into an array of Art class that you define in project 2.

Have the records sorted on ArtID within ArtistID.

Display the columns in the order of artistID, artID, artName, and then artValue.

Sort this once only for every sorting algorithm.

Tabulate and draw the line graphf.

Add the result to p6sortsResult.docx

Turn in:

Your modified version of ArraySorter;

P6Step1.java, p6Step2.java, both output text files, and p6report.docs.

Here is the sample program ArraySorter:

public class ArraySorter

{

// SELECTION SORT

public static

int beginLeftovers = n;

for (int segmentLength = 1; segmentLength <= n/2; segmentLength = 2*segmentLength)

{

beginLeftovers = mergeSegmentPairs(a, tempArray, n, segmentLength);

// Two full segments do not remain at end; the following are possibilites:

// a. one full segment and a partial second segment

// b. one full segment only

// c. one partial segment

// d. nothing is left at end

int endSegment = beginLeftovers + segmentLength - 1;

if (endSegment < n - 1)

// Case a: one full segment and a partial second segment exist, so merge them

merge(a, tempArray, beginLeftovers, endSegment, n-1);

// else Cases b, c, or d: only one full or partial segment is left (leave it in place)

// or nothing is left

} // end for

// merge the sorted leftovers, if any, with the rest of the sorted array

if (beginLeftovers < n)

merge(a, tempArray, 0, beginLeftovers-1, n-1);

} // end iterativeMergeSort

// Merges pairs of segments of a given length within an array

// and returns the index after the last merged pair.

private static >

int mergeSegmentPairs(T[] a, T[] tempArray, int n, int segmentLength)

{

int mergedPairLength = 2 * segmentLength; // Length of two merged segments

int numberOfPairs = n / mergedPairLength;

int beginSegment1 = 0;

for (int count = 1; count <= numberOfPairs; count++)

{

int endSegment1 = beginSegment1 + segmentLength - 1;

int beginSegment2 = endSegment1 + 1;

int endSegment2 = beginSegment2 + segmentLength - 1;

merge(a, tempArray, beginSegment1, endSegment1, endSegment2);

beginSegment1 = endSegment2 + 1;

} // end for

return beginSegment1; // Return index of element after last merged pair

} // end mergeSegmentPairs

private static >

void merge(T[] a, T[] tempArray, int first, int mid, int last)

{

// Two adjacent subarrays are a[beginHalf1..endHalf1] and a[beginHalf2..endHalf2].

int beginHalf1 = first;

int endHalf1 = mid;

int beginHalf2 = mid + 1;

int endHalf2 = last;

// While both subarrays are not empty, copy the

// smaller item into the temporary array

int index = beginHalf1; // Next available location in tempArray

for (; (beginHalf1 <= endHalf1) && (beginHalf2 <= endHalf2); index++)

{ // Invariant: tempArray[beginHalf1..index-1] is in order

if (a[beginHalf1].compareTo(a[beginHalf2]) < 0)

{

tempArray[index] = a[beginHalf1];

beginHalf1++;

}

else

{

tempArray[index] = a[beginHalf2];

beginHalf2++;

} // end if

} // end for

// Finish off the nonempty subarray

// Finish off the first subarray, if necessary

for (; beginHalf1 <= endHalf1; beginHalf1++, index++)

// Invariant: tempArray[beginHalf1..index-1] is in order

tempArray[index] = a[beginHalf1];

// Finish off the second subarray, if necessary

for (; beginHalf2 <= endHalf2; beginHalf2++, index++)

// Invariant: tempa[beginHalf1..index-1] is in order

tempArray[index] = a[beginHalf2];

// Copy the result back into the original array

for (index = first; index <= last; index++)

a[index] = tempArray[index];

} // end merge

// -------------------------------------------------------------------------------

// Swaps the array entries a[i] and a[j].

private static void swap(Object[] a, int i, int j)

{

Object temp = a[i];

a[i] = a[j];

a[j] = temp;

} // end swap

} // end ArraySorter

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!