Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the MergeSort program given in the textbook (program 4.2.6) to support searching subarrays. [MO5.2, MO5.3] Note: The user would give an unsorted list of

Modify the MergeSort program given in the textbook (program 4.2.6) to support searching subarrays. [MO5.2, MO5.3]

Note: The user would give an unsorted list of words as command-line arguments along with the starting and ending index of the subarray that should be sorted. The program should print out a list where the strings between the given indexes are sorted alphabetically.

  • Sample runs would be as follows. >java MergeSort 2 4 toy apply sand bay cat dog fish toy apply bay cat sand dog fish >java MergeSort 0 3 was had him and you his the but and had him was you his the but

Program 4.2.6 Mergesort

public class Merge

{

public static void sort(Comparable[] a)

{

Comparable[] aux = new comparable[a.length];

sort(a, aux , 0 ,a.length);

}

Private static void sort(comparable[ ] a, Comparable[]aux,

{//sort a[lo, hi).

if( hi - lo < = 1) return;

int mid = lo + (hi-lo)/2;

sort(a, aux, lo, mid);

sort(a, aux, mid, hi);

int i = lo, j = mid;

for ( int k = lo; k , hi; k++)

if (i==mid) aux [k] = a[j++];

else if ( j== hi) aux [k]= a[i++];

else if (a[ j ]. compareTo(a[i])<0) aux[k]=a[j++];

else

for (int k = lo; k < hi; k++)

a[ k ] = aux[k];

}

public static void main (string[] args )

{

}

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

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions