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.
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1,...
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1, 0, 2, 1, 5, 0  
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]
Language: Java Implement Merge Sort
Language: Java Implement Merge Sort
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
SHOW WORK Sort the given keys using Counting sort algorithm. Also write the algorithm.          5, 2,...
SHOW WORK Sort the given keys using Counting sort algorithm. Also write the algorithm.          5, 2, 3, 1, 0, 2, 1, 5, 0                                                                  SHOW WORK!
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...
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that...
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that during the final merge, it is not necessary to merge all n elements, but only the elements in positions 1 to k.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT