Question

Reimpliment this bottom-up merge-sort queue code so it does not rely on the "import edu.princeton.cs.algs4.StdRandom" and...

Reimpliment this bottom-up merge-sort queue code so it does not rely on the "import edu.princeton.cs.algs4.StdRandom" and "import edu.princeton.cs.algs4.StdOut"imports:

/*
import java.util.Queue;

import java.util.LinkedList;

import edu.princeton.cs.algs4.StdRandom;

import edu.princeton.cs.algs4.StdOut;


public class BottomUpMergeSort
{
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
Double [] a = new Double [N];
for(int i=0;i<N;i++) {
   a[i]=StdRandom.uniform();
  
}

StdOut.println("Unsorted");
printArr(a);

Queue que=sort(a);
StdOut.println("\nSorted");
BottomUpMergeSort.printQue(que);
}

public static Queue sort(Double[] a) {
   int N=a.length;
  
   Queue->ques=new LinkedList>();
  
   for(int i=0;i<N;i++) {
       Queue m = new LinkedList();
       m.add(a[i]);
       ques.add(m);
      
   }
   while(ques.size()>1) {
       Queue que1=ques.remove();
       Queue que2=ques.remove();
       Queue r=BottomUpMergeSort(que1,que2);
      
       ques.add(r);
      
   }
  
  
}

public static void printArr(Double[]a) {
   for(int i=0;i<a.length;i++) {
       StdOut,println(a[i]);
   }
}
}

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

import java.util.Queue;
import java.util.Scanner;
import java.util.LinkedList;

public class BottomUpMergeSort
{
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
Double [] a = new Double [N];
Scanner sc = new Scanner(System.in);
for(int i=0;i<N;i++) {
a[i]=sc.nextDouble();
  
}

System.out.println("Unsorted");
printArr(a);

Queue que=sort(a);
System.out.println("\nSorted");
BottomUpMergeSort.printQue(que);
}

public static Queue sort(Double[] a) {
int N=a.length;
  
Queue ques=new LinkedList<>();
  
for(int i=0;i<N;i++) {
Queue m = new LinkedList();
m.add(a[i]);
ques.add(m);
  
}
while(ques.size()>1) {
Queue que1=(Queue) ques.remove();
Queue que2=(Queue) ques.remove();
Queue r=BottomUpMergeSort(que1,que2);
  
ques.add(r);
  
}
  
  
}

private static Queue BottomUpMergeSort(Queue aQue1, Queue aQue2) {
   return null;
}

public static void printArr(Double[]a) {
for(int i=0;i<a.length;i++) {
   System.out.println(a[i]);
}
}
}

// Replaced the things with System.out and Scanner

Add a comment
Know the answer?
Add Answer to:
Reimpliment this bottom-up merge-sort queue code so it does not rely on the "import edu.princeton.cs.algs4.StdRandom" and...
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
  • Take the following code and add a joption pane to make it eaiser to read. Can...

    Take the following code and add a joption pane to make it eaiser to read. Can anyone tell me whats wrong with my code? It wont compile JAVA. import java.util.Scanner; import java.util.Queue; import java.util.LinkedList; class que { public static void main(String args[]) { int count=0,min=0,hr=0; String a; Scanner inp=new Scanner(System.in); Queue q=new LinkedList<>(); while(count<10) { System.out.println("Enter your name:"); a=inp.nextLine(); q.add(a); count=count+1; min=min+5; if(min>=60) { hr=hr+1; } System.out.println("You are "+count+" in the queue"); System.out.println("Average wait time is "+hr+" hours and "+min+"...

  • 6. (4 points) The following code for insertion sort has been modified to print out the...

    6. (4 points) The following code for insertion sort has been modified to print out the sorted component of the array during the sort. What will be the output of running the main method of the following code? Try to trace by hand and then verify by executing the code public class Insertion ( public static void sort (String[] a) f int n- a.length; for (int i-1; i < n; i++) f for (int j i; j 0; j--) if...

  • import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements...

    import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> {    private static final long serialVersionUID = -6229569372944782075L;       public void add(K k, V v) {        if (!containsKey(k)) { put(k, new LinkedList<V>()); } get(k).add(v);    }       public V removeFirst(K k) {               if (!containsKey(k)) { return null;        } V value = get(k).removeFirst(); if (get(k).isEmpty()) { super.remove(k); } return value;    }...

  • Please update this java code so 1. A scanner can be used to import the files....

    Please update this java code so 1. A scanner can be used to import the files. Then compute and test with a randomly generated array instead of a file. Compute all possible distinctive pair of values from an an array. [A,B,C, A] A,B A,C B,C - i.e. (A,B)==(B,A) (A, A) is not valid hint: sort the array first Example: input [1, 2, 3, 2, 3, 4, 3] result 4 (with distinct pairs) and 8 (with symmetric pairs import java.util.Arrays; import...

  • LAB: Ticketing service (Queue)

    LAB: Ticketing service (Queue)Given main(), complete the program to add people to a queue. The program should read in a list of people's names including "You" (ending with -1), adding each person to the peopleInQueue queue. Then, remove each person from the queue until "You" is at the head of the queue. Include print statements as shown in the example below.Ex. If the input is:Zadie Smith Tom Sawyer You Louisa Alcott -1the output is:Welcome to the ticketing service...  You are number 3 in the queue. Zadie Smith has purchased a ticket. You are now number 2 Tom Sawyer has purchased a ticket. You are now number 1 You can now purchase your ticket!TicketingService.javaimport java.util.Scanner; import java.util.LinkedList; import java.util.Queue; public class TicketingService {    public static void main (String[] args) {       Scanner scnr = new Scanner(System.in);...

  • 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...

  • Hey i got the program to work but i cant get the merge sorter from big...

    Hey i got the program to work but i cant get the merge sorter from big to little import java.util.*; class MergeSorter {    public static void sort(int[] a)    { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; for (int i = 0; i < first.length; i++) {    first[i] = a[i]; } for (int i = 0; i < second.length; i++) {    second[i] =...

  • import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) {...

    import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...

  • Given main(), complete the program to add people to a queue

    4.14 LAB: Ticketing service (Queue)Given main(), complete the program to add people to a queue. The program should read in a list of people's names including "You" (ending with -1), adding each person to thepeopleInQueue queue. Then, remove each person from the queue until "You" is at the head of the queue. Include print statements as shown in the example below.Ex. If the input isZadie Smith Tom Sawyer You Louisa Alcottthe output isWelcome to the ticketing service...  You are number 3 in the queue. Zadie Smith has purchased a ticket. You are now number 2 Tom Sawyer has purchased a ticket. You are now number 1 You can now purchase your ticket!Code that I have been...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

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