Question

In: Biology

Use the Heap class provided to implement a sort routine in a Main class where the...

Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order.

public class Heap {
        public static final int SIZE = 1025;
        
        public Heap() {
                elt = new Element[SIZE];
                lastLoc = 0;
        }
        
        public void push(String k, Object o) {
                if (!fullCheck()) {
                        lastLoc++;
                        elt[lastLoc] = new Element(k,o);
                        int loc = lastLoc;
                        while (loc > 1 && elt[loc].key.compareTo(elt[loc/2].key) < 0) {
                                Element hold = elt[loc];
                                elt[loc] = elt[loc/2];
                                elt[loc/2] = hold;
                                loc = loc/2;
                        }
                }
        }       

        public void pop() {
                if (!emptyCheck()) {
                        elt[1] = elt[lastLoc];
                        lastLoc--;
                        int loc = 1;
                        while (2*loc <= lastLoc) {
                                loc = 2*loc;
                                if (loc < lastLoc && elt[loc +1].key.compareTo(elt[loc].key) < 0) loc=loc+1;
                                if (elt[loc].key.compareTo(elt[loc/2].key) < 0) {
                                        Element hold = elt[loc/2];
                                        elt[loc/2] = elt[loc];
                                        elt[loc] = hold;
                                }
                        }
                }
        }               
        
        public Object top() {
                return elt[1].payload;
        }

        public Object topKey() {
                return elt[1].key;
        }

        public boolean emptyCheck() {
                return (lastLoc == 0);
        }
        
        public boolean fullCheck() {
                return (lastLoc == SIZE-1);
        }
        
        
        private Element[] elt;
        private int lastLoc;
}
public class Element {
        public Element(String s) {
                key = s;
                payload = null;
        }


public Element(String s, Object obj) {
                key = s;
                payload = obj;
        }
        public String key;
        public Object payload;

}

Solutions

Expert Solution

*******hope it will help you*************


import java.util.*;

class SortStack
{

   public static Stack<Integer> sortstack(Stack<Integer>
                                           input)
   {
       Stack<Integer> tmpStack = new Stack<Integer>();
       while(!input.isEmpty())
       {
  
           int tmp = input.pop();
      

           while(!tmpStack.isEmpty() && tmpStack.peek()
                                               < tmp)
           {
  
           input.push(tmpStack.pop());
           }
          
  
           tmpStack.push(tmp);
       }
       return tmpStack;
   }
  

   public static void main(String args[])
   {
       Stack<Integer> input = new Stack<Integer>();
       input.add(34);
       input.add(3);
       input.add(31);
       input.add(98);
       input.add(92);
       input.add(23);
  
  
       Stack<Integer> tmpStack=sortstack(input);
       System.out.println("Sorted numbers are:");
  
       while (!tmpStack.empty())
       {
           System.out.print(tmpStack.pop()+" ");
       }
   }
}


Related Solutions

Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input to the programs should be ASCII characters 2. Your program should be able to handle upper and lower case letters both 3. The sort should be done in a descending manner 4.Note: Please use array-based representation for these sorting algorithms Please write comment n explain each step clearly as well your program should show what you are taking as input array and what your...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of steps and the CPU running time in a table, please show the code and output Approximation the constant c in the complexity of heap sort (cnlgn) by inspecting the results For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1,...
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework....
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework. Evaluate its performance in terms of the numbers of comparisons and exchanges, and compare it to the performance of the two advanced sorting methods that you have previously implemented. Submit your report with detailed empirical results and a thorough explanation of these results. Which of the three advanced sorting method is the best choice for a) ordered data, b) data in reverse order, and...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm,...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n]. Note:...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority. Enqueue and dequeue in the following order. Function  Name, Priority Enqueue  Joe, 3 Enqueue  Fred,1 Enqueue Tuyet,9 Enqueue  Jose, 6 Dequeue Enqueue  Jing, 2 Enqueue  Xi, 5 Enqueue  Moe, 3 DequeueEnqueue  Miko, 7 Enqueue Vlady, 8 Enqueue Frank, 9 Enqueue  Anny, 3 DequeueEnqueue  Xi, 2 Enqueue  Wali, 2 Enqueue  xChe, 6 Enqueue  xVerra, 8 Dequeue Dequeue Dequeue Dequeue...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
In short, you’re going to implement a linked-list class for storing integers, using a provided main...
In short, you’re going to implement a linked-list class for storing integers, using a provided main program to help you interact and test your work. You’ll want to build the linked-list class function by function, working in “Develop” mode to test out each function you write. main.cpp is a read only file linkedlist.h is the file to work on. main.cpp #include #include #include "linkedlist.h" using namespace std; int main() { linkedlist LL; string cmd; int value, key; // // user...
prove the running time of heap sort.
prove the running time of heap sort.
Please Use C++ to finish as the requirements. Implement a class called SinglyLinkedList. In the main...
Please Use C++ to finish as the requirements. Implement a class called SinglyLinkedList. In the main function, instantiate the SinglyLinkedList class. Your program should provide a user loop and a menu so that the user can access all the operators provided by the SinglyLinkedList class. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
Complete the coding/testing of the heap sort method we began in class. Then modify your program...
Complete the coding/testing of the heap sort method we began in class. Then modify your program so that it outputs a comparison of the actual number of swaps it performs to the predicted number of swaps, and a comparison of the actual sort effort to the predicted and minimum sort effort. The output should be formatted exactly as shown below. Actual swaps = xxxx; Predicted swaps = xxxx Actual sort effort = xxxx; Predicted sort effort = xxxx; Min sort...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT