To implement the mergesort program with the specified requirements, the following steps should be followed:
- Modify the "merge" function to merge the lists in reverse sorted order.
- Prompt the user to input the value of N.
- Take input from the user for the size of the array, N.
- Generate N random integers as the initial input for sorting.
- Implement the mergesort algorithm to sort the array.
- After sorting, output the first five and last five elements of the sorted array.
Modify the "merge" function:
- - Instead of merging the lists in ascending order, modify the function to merge them in reverse sorted order.
- - For example, if A=[3] and B=[8], the merged list C should be [8, 3].
Prompt user for input:
- Display a message asking the user to input the value of N.
User input:
- Accept an integer input from the user, which represents the size of the array to be sorted.
Generate random integers:
- Generate N random integers using a suitable random number generation method.
- These random integers will serve as the initial input for sorting.
Implement mergesort:
- Use a recursive function to implement the mergesort algorithm on the array.
- Split the array into smaller subarrays until each subarray contains only one element.
- Merge the subarrays in reverse sorted order until the entire array is sorted.
Output the sorted elements:
- Display the first five elements and the last five elements of the sorted array.
By following these steps, a mergesort program can be implemented to meet the specified requirements. The modified merge function ensures that the merged list is in reverse sorted order. The program prompts the user for the size of the array and generates random integers for sorting. The mergesort algorithm is then applied to sort the array, and the first five and last five elements of the sorted array are outputted.