Question

In: Computer Science

Please use Java You have to write a programing for the following sorting algorithms in increasing...

Please use Java

You have to write a programing for the following sorting algorithms in increasing order and print required steps in order to explain how your sorting algorithms work.

3. Implement the merge sort algorithm.

With answers in this link (https://www.chegg.com/homework-help/questions-and-answers/write-programing-following-sorting-algorithms-increasing-order-print-required-steps-order--q53916147?trackid=PJ_TOK85), answer the above questions.

Solutions

Expert Solution

Solution:

import java.util.*;
//Java program for merge sort
class Main {
  
//Implemented for merging two subarrays
   //first subarray starts from p and end at q
   //second one start from q and end at r
   void merge_arr(int ar[], int p, int q, int r)
   {
   int i,j;
      
// Finding the size of two arrays thar are required to be merged
       int n1 = q - p + 1;
       int n2 = r - q;

       //creating a temporary array
       int P[] = new int[n1];
       int R[] = new int[n2];

       //copying the data of two array to array L and R
       for (i = 0; i < n1; ++i)
           P[i] = ar[p + i];
       for (j = 0; j < n2; ++j)
           R[j] = ar[q + 1 + j];

       i = 0;
       j = 0;

   //looping through the element and comparing
       int k = p;
       while (j < n2 && i < n1) {
      
//If in array p the element at index i is less than the element in R at index j
           if (P[i] <= R[j]) {
          
//then keeping that element in ar at index k
               ar[k] = P[i];
              
//incrementing i for the next value
               i++;
           }
           else {
          
//otherwise element from array R at index j is stored in ar at index k
               ar[k] = R[j];
              
//incrementing j for next value
               j++;
           }
      
    //incrementing k to store value at next position
           k++;
       }

       //adding remaining value from array P if any left
       while (i < n1) {
           ar[k] = P[i];
           i++;
           k++;
       }

       //adding remaining elements from array R if any left
       while (j < n2) {
           ar[k] = R[j];
           j++;
           k++;
       }
   }

   //function to sort each half of array
   void M_sort(int arr[], int p, int r)
   {
       if (p < r) {
          
// Finding mid point
           int mid = (p + r) / 2;

           // Sorting the first halve of array
           M_sort(arr, p, mid);
          
//sorting the second halve of array
           M_sort(arr, mid + 1, r);
//merging the two halves after sorting
           merge_arr(arr, p, mid, r);
       }
   }

   //function to print the resultant array
   static void print_Array(int ar[])
   {
       int n = ar.length,i;
       for (i = 0; i < n; ++i)
       {
           System.out.print(ar[i] + " ");
       }
       System.out.println("\n");
   }

   // Driver code
   public static void main(String args[])
   {
       int i,n;
       Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in array:");
//Taking number of elements in array as input from user
n = s.nextInt();
int ar[] = new int[n];
System.out.println("Enter elements of array:");

//Taking array elements as input from user
for( i = 0; i < n; i++)
{
ar[i] = s.nextInt();
}

       System.out.println("Array before sorting :");
       print_Array(ar);
//object of class Main created
       Main obj = new Main();
      
//M_sort is called to sort the ARRAY ar
       obj.M_sort(ar, 0, ar.length - 1);

       System.out.println("Array after sorting :");
      
//calling function print_Array to print the sorted array
       print_Array(ar);
   }
}

Steps to solve :

  • Merge sort is based on divide and conquer approach
  • Firstly given array is divided in to two half from the middle of array
  • now to sort the first half of array M_sort () is called which again divided the first half from middle
  • This process take place in same way for second half of array and repeats until the array of single element formed
  • then the elements of array are sorted
  • now these array are merged together to get the resultant array

Note : if you find any error in the above code it may be due to different name of your file than the class name ,so please make sure that you save your file with name Main.java.

If you find my answer helpful please give thumbs up .Thank you


Related Solutions

Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative...
Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative performance of the algorithms for a variety of datasets. Background The skeleton program sorting.cpp contains a main function for testing the operation of several sort algorithms over various data sizes and dataset organisations. The program understands the following arguments: -i insertion sort -s selection sort (default) -q quicksort -a (already) sorted dataset -v reverse-sorted dataset -r random dataset (default) -n no sorting x generate...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
Write a program to compare those two searching algorithms and also compare two sorting algorithms. You...
Write a program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120 integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions. Counter...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms....
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions.Counter for...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms...
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms are shell sort and quick sort. Assume that input data is stored in a text file. Experimenting with a prototype data (integers from 1 to 10) to ensure that your implementation works correctly and the results match expectations. The program will sort what is in the text file and print the amount of comparisons and exchanges for both algorithms.
Please no Plagiarism Steganography: Different type of algorithms, implementations, and write classic algorithms.
Please no Plagiarism Steganography: Different type of algorithms, implementations, and write classic algorithms.
Please no Plagiarism Kleptography: Different type of algorithms, implementations, and write classic algorithms.
Please no Plagiarism Kleptography: Different type of algorithms, implementations, and write classic algorithms.
Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the...
Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the same sorting algorithms as discussed in class / in the textbook. (a) What is the running time of Insertion Sort when all elements in a given list of length N are equal? Explain your answer. (b) Give a Θ-bound on the running time of Mergesort for the case that all elements in a given list of length N are equal (assuming N is a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT