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 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...
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....
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...
(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...
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
Please use the code I provided!! Use either bubble sort or selection sort!! Thank you in...
Please use the code I provided!! Use either bubble sort or selection sort!! Thank you in advance This lab involves adding a sorting function to an existing C++ template. In this module, a file is provided for you -- SortableBag.h -- that implements a simple "bag" (unsorted collection of numbers) structure with array storage. Part 1 Add a sort() method to this template, which should not return any values or take any arguments ('void' for both the return type and...
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition()...
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition() and quickSort(). You are expected to call swap() within partition(), to call partition() within quickSort(), and you are not expected to declare/ implement other additional functions nor change the main() function. OPTIONAL: If you don’t need/ want to use swap() in your implementation, that is fine. Just delete/ comment it. quickSort.cpp #include <iostream> using namespace std;    // A helper function to facilitate swapping...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAMM
Part A Write a 6-8 sentence summary explaining how you can use the Heap Sort algorithms...
Part A Write a 6-8 sentence summary explaining how you can use the Heap Sort algorithms to solve a real world problem. In your summary, provide a descriptive or visual explanation of your heapsort solution. Part B For this portion of the assignment you will design an algorithm to implement the solution described in part A. Your algorithm must be written in pseudocode. Note: Sample HeapSort Algorithm Pseudocode: Heapsort(A as array) BuildHeap(A) for i = n to 1 swap(A[1], A[i])...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT