Question

please help me with this question. This is required to be written in Java.
Objective The objective of this programming assignment is to design and implement in Java the Merge-Sort algorithm presented

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

import java.util.Scanner;

import java.util.*;

import java.text.*;

class Main

{

void mergetech(int numarr[], int l, int m, int r)

{

//variable declaration

int t1 = m - l + 1;

int t2 = r - m;

int leftarr[] = new int [t1];

int rightarr[] = new int [t2];

for (int i=0; i<t1; ++i)

leftarr[i] = numarr[l + i];

for (int j=0; j<t2; ++j)

rightarr[j] = numarr[m + 1+ j];

int i = 0, j = 0;

int k = l;

while (i < t1 && j < t2)

{

if (leftarr[i] <= rightarr[j])

{

numarr[k] = leftarr[i];

i++;

}

else

{

numarr[k] = rightarr[j];

j++;

}

k++;

}

while (i < t1)

{

numarr[k] = leftarr[i];

i++;

k++;

}

while (j < t2)

{

numarr[k] = rightarr[j];

j++;

k++;

}

}

//sorting method

void sorting(int numarr[], int l, int r)

{

if (l < r)

{

int m = (l+r)/2;

sorting(numarr, l, m); //invoking the function

sorting(numarr , m+1, r);

mergetech(numarr, l, m, r);

}

}

static void display(int numarr[]) //display fuction

{

int n = numarr.length;

for (int i=0; i<n; ++i) //displaying the values

System.out.print(numarr[i] + " ");

System.out.println();

}

public static void main(String args[]) //main function

{

Scanner sc=new Scanner(System.in);

int i,n;

//start of execution time

long start = System.currentTimeMillis();

//accepting the length of the array

System.out.println("Enter the length of the array");

n=sc.nextInt();

int numarr[]= new int[n];

//accepting the elements of the array

System.out.println("Enter the elements of the array");

for(i=0;i<n;i++)

{

numarr[i]=sc.nextInt();

}

System.out.println("Before Sorting");

System.out.println("Input Array");

display(numarr);

Main ob = new Main(); //object of the array

ob.sorting(numarr, 0, n-1); //calling the sorting function

//end of execution time

long end = System.currentTimeMillis();

System.out.println("After Sorting");

System.out.println(" Output array");

display(numarr);

NumberFormat formatter = new DecimalFormat("#0.00000");

//displaying the execution time

System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");

}

}

OUTPUT

Enter the length of the array Enter the elements of the array 99 34 56 Before Sorting Input Array 99 34 2 56 1 After SortingEnter the length of the array 10 Enter the elements of the array 77 Before Sorting Input Array 99 67 34 1 2 44 65 33 31 77 Af

Merge sort is a type of recursive algorithm
Hence
recurrence relation T(n)=2T(n/2) + Theta(n)

If we see this the merge sort always divides the array into two parts and then it takes time which is linear in account to join the arrays.
Hence the time complexity for both best ,average and worst case is same which is Theta(nLogn).

Add a comment
Know the answer?
Add Answer to:
please help me with this question. This is required to be written in Java. Objective The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

  • Java Merge sort algorithm

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Part A Analyze the following recurrences and show their time complexity functions using (I) iteration method...

    Part A Analyze the following recurrences and show their time complexity functions using (I) iteration method and (2) Master Theorem. AI. T(n) = 2T 3 A2. T(n) = 3T 2n АЗ. Т(п) — Т(п — 2) + 3 А4. Т(п) — 2Т (п — 1) + 1 A5. T(n)= 4T +n log n A6. T(n) = 3T +n log n n2 A7. T(n) = 27 Part B Do 2.3-4 (р39) and Problem 2-1 (р39) Part C Implement MERGE-SORT() algorithm that...

  • Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

  • Lab Overview: In this lab, you will work with merge sort. Objectives: Identify a problem that...

    Lab Overview: In this lab, you will work with merge sort. Objectives: Identify a problem that can efficiently be solved with merge sort. Implement the merge sort algorithm Determine and summarize the algorithmic run-time of the merge sort algorithm. Specifications: As you saw from your readings and exercises, Merge Sort is based upon the idea of divide-and-conquer. If implemented correctly, your merge sort algorithm should have a time complexity of O(n*log(n)). Your task for this lab is to write a...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java....

    Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java. Your program should receive its input from a file named "input.txt", which contains one integer per line. It should produce a sorted output file named "output.txt". Include a main method which demonstrates that your algorithm works.

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...

    need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT