0% found this document useful (0 votes)
16 views5 pages

Cs502 Assignments

Uploaded by

ahmadraza6491
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Cs502 Assignments

Uploaded by

ahmadraza6491
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Course Title: [CS502]

Submitted By: [Ahmad Raza]

STUDENT ID: [bc210407232]

We need to insert the node 35 into the given max heap and apply the heapify procedure to maintain the
max heap property.

Initial Max Heap:

38

/ \

13 24

/ \ / \

1 7 5 NULL

Insert the New Node

The new node 35 is inserted at the next available position in the tree (as a child of 24) to maintain the
complete binary tree structure:

38

/ \

13 24

/ \ / \

1 7 5 35
Apply Heapify (Bottom-Up)

We now compare the inserted node 35 with its parent 24 and swap them because 35 > 24. The tree
becomes:

38

/ \

13 35

/ \ / \

1 7 5 24

Next, compare 35 with its parent 38. Since 35 < 38, no further swaps are needed, and the max heap
property is restored.

Final Max Heap:

38

/ \

13 35

/ \ / \

1 7 5 24

This is the final max heap after inserting 35.

Question 2:

Given below is an unsorted array. You are required to sort the following given array step by step,
using Merge Sort (ascending order). You are required to correctly fill the empty cells given below using
the divide and conquer strategy of merge sort algorithm.

Solution.
Divide the Array

Start dividing the array into halves:

[38, 27, 43, 3]

----------------

| |

[38, 27] [43, 3]

---

Step 2: Divide Further

Keep splitting until each subarray has one element:

[38, 27] [43, 3]

| |

----- -----

| | | |

[38] [27] [43] [3]

Merge and Sort

Now, start merging the subarrays while sorting them:


Merge [38] and [27]:

Compare 38 and 27 → [27, 38]

[27, 38]

Merge [43] and [3]:

Compare 43 and 3 → [3, 43]

[3, 43]

Merge Final Two Sorted Subarrays

Merge [27, 38] and [3, 43]:

1. Compare 27 and 3 → Add 3.

2. Compare 27 and 43 → Add 27.

3. Compare 38 and 43 → Add 38.

4. Add remaining element 43.

Result: [3, 27, 38, 43]

[3, 27, 38, 43]

Final Diagram:

Here is the fully labeled merge process:


This is the final sorted array!

You might also like