- Kany 4 The Following Figure Shows An Iterative Mergsort Algorithm A A2 An Is The Set Of Numbers Needed To Be S 1 (357.9 KiB) Viewed 28 times
KANY 4. The following figure shows an iterative-Mergsort algorithm. a₁, a2, ..., an is the set of numbers needed to be s
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
KANY 4. The following figure shows an iterative-Mergsort algorithm. a₁, a2, ..., an is the set of numbers needed to be s
KANY 4. The following figure shows an iterative-Mergsort algorithm. a₁, a2, ..., an is the set of numbers needed to be sorted. Initially, each element of a1, a2, anis considered as an array separately and each array is added to the queue Q. Then, removing two arrays from the front of the queue, merging them, and putting the result array at the end of the queue. The process is iteratively done until there is only one array in the queue. When merging two arrays (merge), they are done recursively and the first numbers of all arrays are compared and the smaller one is removed every time. The operation inject adds an element to the end of the queue while eject removes and returns the element at the front of the queue. Let the unsorted array contains 30, 78, 16, 47, 20, 15, 6, 12, 82. Please show states of the Q at each step, in other words, show the changes of Q (12¹). function iterative-mergesort (a[1...n]) Input: elements a1,a2....,an to be sorted Q=[] (empty queue) for i=1 to n: inject(Q. [a]) while Q1: inject(Q. merge(eject(Q). eject(Q))) return eject(Q)