Question

In: Computer Science

USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that...

USING JAVA

Almost sort the array using Merge Sort. Perform Merge Sort as usual except that during the final merge, it is not necessary to merge all n elements, but only the elements in positions 1 to k.

Solutions

Expert Solution

Java code:

import java.util.Scanner;
public class MergeSort{

//merge function
public static void merge(int arr[],int left,int mid,int right)
{
int i,j,k;
int sz1=mid-left+1;
int sz2=right-mid;
int L[]=new int[sz1];
int R[]=new int[sz2];
for (i = 0;i<sz1;i++)
{
   L[i]=arr[left+i];
   }
for (j = 0;j<sz2;j++)
{
   R[j]=arr[mid+1+j];
   }
  
  
  
i = 0;
j = 0;
k = left;
while (i<sz1 && j<sz2)
{
if (L[i] <= R[j])
{
arr[k]=L[i];
i++;
}
else
{
arr[k]=R[j];
j++;
}
k++;
  
}
  
while(i<sz1)
{
arr[k]=L[i];
i++;
k++;
}
while(j<sz2)
{
arr[k]=R[j];
j++;
k++;
}
  
}


//merge sort function
public static void mergeSort(int arr[],int left,int right)
{

if(left<right)
{
int mid=left+(right-left)/2;
mergeSort(arr,left,mid);
mergeSort(arr,mid+1,right);
merge(arr,left,mid,right);
}

  
}

//main function
public static void main(String[] args) {
   int data[]=new int[100];
   int n,k,i;
   Scanner scan = new Scanner(System.in);

   System.out.print("Enter number of items in array: ");
   n=scan.nextInt(); //store total number of items in array in variable n
   System.out.print("Enter array items: ");
   for(i=0;i<n;i++)
   {
       data[i]=scan.nextInt();   //stored input array items in array
   }
   System.out.print("Enter value of k: ");
   k=scan.nextInt(); //enter position after which array is sorted

   System.out.println("Initial array elements: ");
   for(i=0;i<n;i++)
   {
       System.out.print(data[i]+" ");
   }
  

   mergeSort(data,0,k-1); //call mergesort for the array from oth position to k-1

   System.out.println("\nArray after selection sort: ");
   for(i=0;i<n;i++)
   {
       System.out.print(data[i]+" ");
   }
}

}

Output:


Related Solutions

Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in place. * * @param students array to be sorted, can be empty, but cannot be null */ public static void sortGPA(Student[] students) { // TODO: implement this } Student class: public class Student extends Person { private double GPA; public Student(String lastName, String firstName, double gpa) { super(lastName, firstName); this.GPA = gpa; } public double getGPA() { return GPA; } @Override public boolean equals(Object...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the...
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the array into 2 partitions, it divides the array into p partitions. It recursively sorts each of the p partitions and then merges the results doing a p-way merge (except, of course, for the base case of a singleton or empty array). (a) Write the recurrence for T(n) for M&M sort for the case where p is a constant. Then, give a closed-form solution to...
Language: Java Implement Merge Sort
Language: Java Implement Merge Sort
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will...
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will take to sort with random data sets of users input numbers.
PYTHON Perform a series of benchmarking tests on merge-sort and quick-sort to determine which one is...
PYTHON Perform a series of benchmarking tests on merge-sort and quick-sort to determine which one is faster. Your tests should include sequences that are “random” as well as “almost” sorted.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT