package mergesort; import java.util.Scanner; /* Class MergeSort */ public class
ID: 3706561 • Letter: P
Question
package mergesort;
import java.util.Scanner;
/* Class MergeSort */
public class MergeSort
{
/* Merge Sort function */
public static void sort( int[] array, int low, int high )
{
int diff;
int midpoint;
/* Calculate Difference between low and high */
diff = high-low;
/* Recursion is called until diff is less than or equal to 1 */
/* Return if less than or equal to 1 */
if ( diff <= 1 )
{
return;
}
/* Calculate the midpoint between low and diff */
midpoint = low + diff/2;
// We will call sort recursively here, once for the lower half of
the
// provided array and once for the upper half of the array
System.out.printf( "Calling sort: low [%d], high [%d] ", low,
midpoint );
// Sort Lower Half of Array
System.out.printf( "Calling sort: low [%d], high [%d] ", midpoint,
high );
// Sort Higher Half of Array
// Merge Two Sorted Subarrays
// Create temporary array and temporary variables
int[] temp = new int[diff];
int i = low, j = midpoint;
System.out.printf( "Merging %d Elements ", diff );
for ( int k=0; k<diff; k++ )
{
// Fill in temporary array as necessary
if ( i == midpoint )
{
}
else if ( j == high )
{
}
else if ( array[j] < array[i] )
{
}
else
{
}
}
System.out.print( "Current Array: " );
for ( int k=0; k<diff; k++ )
{
// Fill in array with temporary array elements
System.out.printf( "%d ", array[low+k] );
}
System.out.println();
}
/* Main method */
public static void main(String[] args)
{
Scanner keyboard = new Scanner( System.in );
int elements, lp;
int intArray[] = null;
System.out.println( "Merge Sort Test " );
/* Accept number of elements */
System.out.println( "Enter Number of Integer Elements: " );
elements = keyboard.nextInt();
/* Create array of n elements */
intArray = new int[ elements ];
/* Read Elements from the keyboard */
System.out.println( " Enter " + elements + " integer elements" );
for ( lp=0; lp<elements; lp++ )
{
System.out.print( "Element " + ( lp+1 ) + ": " );
intArray[lp] = keyboard.nextInt();
}
/* Call method sort */
sort( intArray, 0, elements );
/* Print sorted Array */
System.out.println( " Elements After Sorting" );
for ( lp=0; lp<elements; lp++ )
{
System.out.print( intArray[lp] + " " );
}
System.out.println();
}
}
Page of 2 ZOOM The objective of this assignment is to develop a merge sort method, which will sort an array of integer values Lab Overview The merge sort is an efficient, general-purpose, comparison-based sorting algorithm. The merge sort is a divide and conquer algorithm. As an example of a merge sort, the algorithm will first divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all of the elements are sorted and merged 7 1 3 56 2 4 78 1 234 5 6 78 Lab Instructions A Create a new Java Application called MergeSort. This lab template file can be found on Canvas Download the template file from Canvas, follow the tasks assigned in an attempt to put the proper MergeSort algorithm together. The debug output, as shown below, has been left in the source file to assist you in determining what to do next. Sample Output The sample output below shows the MergeSort algorithm, with logs of debug output to show the progression of the algorithmExplanation / Answer
Hi Dear,
Please find my implementation.
Merge Sort Test
Enter Number of Integer Elements:
5
Enter 5 integer elements
Element 1: 1
Element 2: 5
Element 3: 2
Element 4: 4
Element 5: 3
Calling sort: low [0], high [2]
Calling sort: low [0], high [1]
Calling sort: low [1], high [2]
Merging 2 Elements
Current Array: 1 5
Calling sort: low [2], high [5]
Calling sort: low [2], high [3]
Calling sort: low [3], high [5]
Calling sort: low [3], high [4]
Calling sort: low [4], high [5]
Merging 2 Elements
Current Array: 3 4
Merging 3 Elements
Current Array: 2 3 4
Merging 5 Elements
Current Array: 1 2 3 4 5
Elements After Sorting
1 2 3 4 5
Process finished with exit code 0
Please DONT forgot to rate my answer. We are working hard for you Guys!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.