Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Above the java code below, write a main method to merge sort an array of numbers. Write the merge part to do this. public static

Above the java code below, write a main method to merge sort an array of numbers. Write the merge part to do this.

public static

mergeSort(a, tempArray, first, last);

} // end mergeSort

private static >

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

{

if (first < last)

{ // sort each half

int mid = first + (last - first) / 2; // Index of midpoint

mergeSort(a, first, mid); // Sort left half array[first..mid]

mergeSort(a, mid + 1, last); // Sort right half array[mid+1..last]

if (a[mid].compareTo(a[mid + 1]) > 0) // Question 2, Chapter 9

merge(a, tempArray, first, mid, last); // merge the two halves

// else skip merge step

} // end if

} // end mergeSort

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

}

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_2

Step: 3

blur-text-image_3

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

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago