{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# WA1 - Comp271 Written Assignment 1 Name Gao Yuan ID...

This preview shows pages 1–2. Sign up to view the full content.

Comp271 Written Assignment 1 Name: Gao, Yuan ID: 05734371 Email: cs gyx Problem 2 The method is like Mergesort. We will divide the array into parts and count the inversions of each part. Algorithm: Divide: Divide each array into 2 sub-arrays of the same size until each sub-array contains only one element Conquer: Keep a counter to count the number of inversions. The initial value of the counter is 0 Keep 2 pointers pointer to the first element left sub-array and the first element of the right sub-array for each array. When combine 2 sub-arrays, we have to prepare a new array whose size is the sum of the sizes of the 2 sub-arrays Compare elements pointed by the pointers. 1. If the element in the left sub-array is smaller, put it in the first empty position of the new array, move the left pointer to the next element 2. If the element in the right sub-array is smaller, put it in the first empty position of the new array, move the right pointer to the next element 2.0Increase the counter by the number of elements left in the left sub-array Until all the elements in the 2 sub-arrays have copied to the new array, then begin to process the new arrays If one sub-array is empty, we can move all the left elements in the other sub-array to the new array without comparisons or increasing the counter Thus when we combined and get the whole array which has the size of the original array, we can get the number of the inversions from the counter. Time complexity: Divide: Divide each array into 2 sub-arrays: O(n) Thus total time complexity in divide part is O(n) Conquer: Keep a counter: O(1) Keep 2 pointers: O(2) Maximum comparisons need: O(n) for each level, O(nlogn) in total Maximum pointers’ move : O(n) for each level, O(nlogn) in total Increase the counter: O(n/2) for each level, O(n/2logn) in total Copy the elements in to the new array: O(n) for each level, O(nlogn) in total Thus total time complexity in conquer part is O(nlogn) Problem 3 (a) The method is like Mergesort. We will divide the array into parts and search for pairs whose sum is exactly x. Divide: Divide each array into 2 sub-arrays of the same size until each sub-array contains only one element Conquer: First we combine 2 sub-arrays and then search for the pairs of elements Keep 2 pointers pointing to the first element left sub-array and the first element of the right sub-array for each array.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern