In: Computer Science
1, Considering using sorted and unsorted array as priority queue to do sorting. For an input array, we insert each element to a PQ and remove them out to an output array. Please write the arrays below:
Input array: 2,7,5,3
Input array: 2,7,5,3
2, If I use upheap strategy to construct a heap(array as implementation) from above array, what is the array of the heap of each step?
The various instances of the array , after the various operations as required by question is presented below:
Q1)
Input array: 2,7,5,3
1. Sorted PQ after 1st Insertion: [2]
2. Sorted PQ after 2nd Insertion: [2,7]
3. Sorted PQ after 3rd Insertion: [2,5,7]
4. Sorted PQ after 4th Insertion: [2,3,5,7]
5. Output array after 1st removeMin: [3,5,7]
6. Output array after 2nd removeMin: [5,7]
7. Output array after 3rd removeMin:[7]
8. Output array after 4th removeMin:[ ]
Input array: 2,7,5,3
1. Unsorted PQ after 1st Insertion: [2]
2. Unsorted PQ after 2nd Insertion: [2,7]
3. Unsorted PQ after 3rd Insertion: [2,7,5]
4. Unsorted PQ after 4th Insertion: [2,7,5,3]
5. Output array after 1st removeMin: [7,5,3]
6. Output array after 2nd removeMin: [7,5]
7. Output array after 3rd removeMin: [7]
8. Output array after 4th removeMin: [ ]
Q2)
1. Step 1: [2]
2. Step 2: [7,2]
3. Step 3: [7, 2, 5]
4. Step 4: [7,3,5,2]