Question: Question 2: (in python) Input: [5, 2, 8, 3, 1, 6, 9, 7] 1. Define the mergeSort function that takes an array arr as input.

Question 2: (in python) Input: [5, 2, 8, 3, 1, 6, 9, 7]

1. Define the mergeSort function that takes an array arr as input. 2. If the length of arr is greater than 1, perform the following steps: 1. Find the middle index of arr. 2. Divide arr into two halves: L containing the left half of arr and R containing the right half of arr. 3. Recursively call mergeSort on L. 4. Recursively call mergeSort on R. 5. Initialize variables i, j, and k to 0. 6. While i is less than the length of L and j is less than the length of R, compare L[i] and R[j]. If L[i] is less than or equal to R[j], set arr[k] to L[i] and increment i. If L[i] is greater than R[j], set arr[k] to R[j] and increment j. Increment k after each comparison. 7. If there are any remaining elements in L, copy them to arr. 8. If there are any remaining elements in R, copy them to arr. 3. Define the printList function that takes an array arr as input and prints out each element of arr. 4. In the main block, define an list arr, print it using the printList function, call mergeSort on arr, and then print arr using the printList function again to show the sorted array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!