Concept explainers
Algorithm: Counting Inversions using Modified Merge Sort
Input: An array of distinct numbers, arr[1...n]
Output: The number of inversions in the array
1. Define a function merge_sort_and_count_inversions(arr):
- If the length of arr is less than or equal to 1, return arr and 0.
- Calculate mid as the integer division of the length of arr by 2.
- Recursively call merge_sort_and_count_inversions on the left half of arr and store the sorted array and inversion count in left_half and left_count.
- Recursively call merge_sort_and_count_inversions on the right half of arr and store the sorted array and inversion count in right_half and right_count.
- Call merge_and_count_inversions with left_half and right_half and store the sorted array and merge_count.
- Return the sorted array and the sum of left_count, right_count, and merge_count.
2. Define a function merge_and_count_inversions(left, right):
- Initialize an empty list merged[] and set inversions to 0.
- Initialize two pointers i and j to 0.
- While i is less than the length of left and j is less than the length of right:
- If left[i] is less than or equal to right[j], append left[i] to merged and increment i by 1.
- Else, append right[j] to merged, increment j by 1, and add (length of left - i) to inversions.
- Extend merged with the remaining elements of left and right.
- Return merged and inversions.
3. Define a function count_inversions(arr):
- Call merge_sort_and_count_inversions on arr and discard the sorted array, keeping only the inversions count.
- Return the inversions count.
4. Get user input for the array as a space-separated string.
5. Split the input string to obtain the array of integers.
6. Call count_inversions with the input array.
7. Print the number of inversions.
8. End.
Step by stepSolved in 3 steps with 1 images
- Below is the exercise for unsorted arrays. True or False: For each statement below, indicate whether you think it is True or False 3) For the insert function, if the array is empty, there are no comparison operations that need to be performed and you can immediately add the new element 5) Because the update algorithm depends on using linear search, its performance is O(1) in the worst case scenario 6) If you search for and delete an element in an unsorted array and then shift the rest of the elements to fill the hole, the worst case performance is O(n) 7) If you search for and delete an element in an unsorted array and then move the last element to fill the hole, the worst case performance is O(n)arrow_forwardVery confused about this problem. Any help is appercaitedarrow_forwardPYTHON: Please provide a screenshot of your code in Python. Thank you!arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education