Sort the following number using quick sort and heap sort.10, 15, 98, 78, 115, 124, 42, 20, 28, 118, 160, 054. With suitable example explain various types of tree traversal methods.
Question
Sort the following number using quick sort and heap sort.10, 15, 98, 78, 115, 124, 42, 20, 28, 118, 160, 054. With suitable example explain various types of tree traversal methods.
Solution
To sort the given numbers using quick sort, we first need to understand the algorithm. Quick sort is a divide-and-conquer algorithm that works by selecting a pivot element and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then recursively sorted.
Here are the steps to sort the given numbers using quick sort:
- Choose a pivot element from the array. In this case, let's choose the last element, 054.
- Partition the array into two sub-arrays: one with elements less than the pivot and one with elements greater than the pivot. In this case, the partitioning would look like this:
- Lesser elements: 10, 15, 42, 20, 28
- Greater elements: 98, 78, 115, 124, 118, 160
- Recursively apply quick sort to the sub-arrays. For the lesser elements, the sorted order would be: 10, 15, 20, 28, 42. For the greater elements, the sorted order would be: 78, 98, 115, 118, 124, 160.
- Combine the sorted sub-arrays to get the final sorted array: 10, 15, 20, 28, 42, 78, 98, 115, 118, 124, 160.
Now, let's move on to heap sort. Heap sort is another comparison-based sorting algorithm that uses a binary heap data structure. It works by first building a max-heap from the array and then repeatedly extracting the maximum element from the heap and placing it at the end of the sorted array.
Here are the steps to sort the given numbers using heap sort:
- Build a max-heap from the array. The max-heap property states that for any node i, the value of the parent node is greater than or equal to the values of its children nodes. In this case, the max-heap would look like this:
- 160, 124, 115, 118, 98, 78, 42, 20, 28, 10, 15, 054
- Swap the root node (the maximum element) with the last element in the heap and remove it from the heap. In this case, we swap 160 with 054 and remove 160 from the heap.
- Heapify the remaining elements to maintain the max-heap property. In this case, the heap would look like this:
- 124, 118, 115, 28, 98, 78, 42, 20, 10, 15
- Repeat steps 2 and 3 until the heap is empty.
- The sorted array would be the elements extracted from the heap in reverse order: 160, 124, 118, 115, 98, 78, 42, 28, 20, 15, 10, 054.
Now, let's discuss the various types of tree traversal methods:
-
Inorder Traversal: In this method, we first traverse the left subtree, then visit the root node, and finally traverse the right subtree. This results in a sorted order for a binary search tree.
-
Preorder Traversal: In this method, we first visit the root node, then traverse the left subtree, and finally traverse the right subtree.
-
Postorder Traversal: In this method, we first traverse the left subtree, then traverse the right subtree, and finally visit the root node.
These traversal methods can be applied to various types of trees, including binary trees, binary search trees, and n-ary trees. They are useful for accessing and processing the elements of a tree in a specific order.
Similar Questions
State and explain various types if binary tress2. Construct the AVL Tree from the following10, 15, 98, 78, 115, 124, 42, 20, 28, 118, 160, 053. Sort the following number using quick sort and heap sort.10, 15, 98, 78, 115, 124, 42, 20, 28, 118, 160, 05
Sort the following list of numbers using BST sort: 144, 151, 162, 70, 41, 120, 158, 124, 123, 93, 12, 23, 68.Display the numbers in the sorted order. Draw the tree at each step and give the step-by-step workout.
Program to implement heap sort
Sort the given list of numbers in ascending order. Demonstrate the first pass/sweep sort algorithm. 43 18 9 76 89 1
Write an algorithm/pseudocode to sort elements using Heap sort technique?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.