Question

In: Computer Science

Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...

Java language

1. Sort any 10 keys using Min Heap.

2. Sort any 10 keys using Max Heap.

Solutions

Expert Solution

Below is the code in JAVA for both the parts, sorting 10 keys using min_heap and max_heap respectively. In one program only, we have written logic for both types of heap. Since, it is nowhere mentioned in the question to construct heaps from the scratch, therefore, used priority queue inbuilt function in JAVA for which the import is "import java.util.*" as it is in util package. Sample output for sorted elements using both the heaps is attached at the end. We have used "poll" function of the priority queue which works exactly like "extract_min or extract_max" in min heap and max heap respectively, which means it returns the top element and remove it from the collection(heap, or priority queue). Priority queue implementation in JAVA is basically min_heap ,so to make it max heap we can use "Collections.reverseOrder()" as ---- PriorityQueue<Integer> max_heap = new PriorityQueue<Integer>(Collections.reverseOrder()).
To print the element of heap we use peek() method which just return the root element without removing it.

import java.util.*;
public class Main
{
        public static void main(String[] args) {

        //************ starting of sorting using MIN_HEAP ***************

                PriorityQueue<Integer> min_heap = new PriorityQueue<Integer>();
                System.out.print("add elements in to min heap:\n");
        
        // prompting user to input the elements in heap.
                Scanner sc = new Scanner(System.in);
                for(int i=0; i<10; i++){
                    min_heap.add(sc.nextInt());
                }
                
                System.out.print("sorted keys in ascending order:\n");
        
        // printing the top element every time till heap is empty. 
                while(!min_heap.isEmpty()){
                    System.out.print(min_heap.peek()+" ");
                    min_heap.poll();
                }
                
        
        System.out.print("\n");
        System.out.print("\n");

        // *********** starting of sorting using MAX_HEAP ***************

        System.out.print("add elements in to max heap:\n");
        
        PriorityQueue<Integer> max_heap = new PriorityQueue<Integer>(Collections.reverseOrder());

        // prompting user to input the elements in heap.
                for(int i=0; i<10; i++){
                    max_heap.add(sc.nextInt());
                }
                
                // declaring array to hold elements to print the sequence in ascending order.
                int arr[]=new int[10];
                int i = 0;
                
                System.out.print("sorted keys in descending order:\n");

        // printing the top element every time till heap is empty.
        // also storing elements in array to print sequence in ascending order.
                while(!max_heap.isEmpty()){
                    System.out.print(max_heap.peek()+" ");
                    arr[i++] = (int)max_heap.poll();
                }
                System.out.print("\n");
                System.out.print("sorted keys in ascending order:\n");
       
        // printing elements of array which is in ascending order.
                for(int idx = 9; idx>=0; idx--){
                    System.out.print(arr[idx]+" ");
                }
        }
}

Sample Output:-


Related Solutions

Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
Write a program to sort the following using HeapSort. Use any language you want. A=[10, 8,...
Write a program to sort the following using HeapSort. Use any language you want. A=[10, 8, 7, 9,16, 14, 3, 2, 4, 1]
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                     No code or programs, please. Manually solve the problem, please. Thanks
Write a program, using any language you want, to sort the following using QuickSort: {3, 4,...
Write a program, using any language you want, to sort the following using QuickSort: {3, 4, 5,1, 2, 6, 7, 8}
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...
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...
Suppose H is a binary min-heap with n nodes, Prove the following facts: Any algorithm that...
Suppose H is a binary min-heap with n nodes, Prove the following facts: Any algorithm that must nd the maximum key of the heap must search all of the leaf nodes.
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying to understand how this works. Thank you
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do his manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please give your answer keeping this in your mind.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT