Question

In: Computer Science

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.

Solutions

Expert Solution

import java.io.*;
import java.util.*;
public class Main
{
////// main function as tester function ///////
   public static void main(String[] args) {

       int n = 5; /// size of list
       Name[] arr = new Name[n]; /// array if Name objects

////// adding names and corresponding frequencies /////
       arr[0] = new Name("apple", 10);
       arr[1] = new Name("orange", 8);
       arr[2] = new Name("banana", 1);
       arr[3] = new Name("potato", 4);
       arr[4] = new Name("onion", 0);
  
//// calling sorting function /////
       mergeSort(arr, n);
  
  
////// printing the sorted array of Name objects based on frequency
       for(int i = 0;i<n;i++)
       {
           System.out.println(arr[i].name + " ---- " + arr[i].frequency);
       }
   }


   public static void mergeSort(Name[] arr, int n)
   {
       if(n<2) return; //// base condition to terminate the recursion
      
       int mid = n/2;
      
       //////// division of array into 2 arrays //////
       Name[] L = new Name[mid];
       Name[] R = new Name[n-mid];
  
int i = 0;
       for(i = 0;i<mid;i++)
       {
           L[i] = arr[i];
       }

       for(int j = 0;j<n-mid;j++)
       {
           R[j] = arr[i];
           i++;
       }

  
///// recursively sorting the splited arrays
       mergeSort(L, mid);
       mergeSort(R, n-mid);
  
///// merging the splited arrays to final sorted array
       merge(L, R, arr);
   }
  
  
//////////// merge logic //////////////
   public static void merge(Name[] L, Name[] R, Name[] arr)
   {
       int l = L.length;
       int r = R.length;
       int n = arr.length;

       int i = 0;
       int j = 0;
       int k = 0;

       while(i<l && j<r && k<n)
       {
           if(L[i].frequency < R[j].frequency)
           {
               arr[k] = L[i];
               i++;
           }
           else
           {
               arr[k] = R[j];
               j++;
           }

           k++;
       }

       while (i<l && k<n)
       {
           arr[k] = L[i];
           i++;
           k++;
       }

       while (j<r && k<n)
       {
           arr[k] = R[j];
           j++;
           k++;
       }
   }

}


////// name class which contains name and the frequency of corresponding name
class Name
{
   String name;
   int frequency;

   public Name(String name, int frequency)
   {
       this.name = name;
       this.frequency = frequency;
   }
}


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.
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
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.
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6,...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45
In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to sort it in ascending order
Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed...
Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed up the computation. If the pseudocode in Wikipedia (https://en.wikipedia.org/wiki/Library_sort) is not enough, you can download the research paper from (https://arxiv. org/pdf/cs/0407003.pdf). Below is the algorithm and pseudo code. Implementation Algorithm Let us say we have an array of n elements. We choose the gap we intend to give. Then we would have a final array of size (1 + ε)n. The algorithm works in...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT