Homework Solution: 3. (25 points) Let A[1..] be an array of n distinct numbers. If i A, the pair (i,j) is called an inversion of A. (a) Lis…

    3. (25 points) Let A[1..] be an array of n distinct numbers. If i < j and A>A, the pair (i,j) is called an inversion of A. (a) List all the inversions of the array(2,3, 8,6, l). b) What array with elements from the set 1,2,n has the most inversions How many does it have? (c) What is the relationship between the running time of INSERTION SORT (see question 2) and the ns in the input array? Justify your answer. (d) Suppose we are comparing implementations of insertion sort and merge sort (a more advanced sorting algorithm, which we will learn about later in the semester) on the same machine. For inputs of size n, insertion sort runs in 8n2 steps, while merge sort runs in 64n log2n steps. For which values of n does insertion sort beat merge sort?
    3. (25 points) Let A[1..] be an array of n distinct numbers. If i A, the pair (i,j) is called an inversion of A. (a) List all the inversions of the array(2,3, 8,6, l). b) What array with elements from the set 1,2,n has the most inversions How many does it have? (c) What is the relationship between the running time of INSERTION SORT (see question 2) and the ns in the input array? Justify your answer. (d) Suppose we are comparing implementations of insertion sort and merge sort (a more advanced sorting algorithm, which we will learn about later in the semester) on the same machine. For inputs of size n, insertion sort runs in 8n2 steps, while merge sort runs in 64n log2n steps. For which values of n does insertion sort beat merge sort?

    Expert Answer

     
    Hi, Inversions are defined as when i<j then a[i]>a[j]

    3. (25 points) Let A[1..] be an invest of n disjoined bulk. If i < j and A>A, the brace (i,j) is determined an reversal of A. (a) List complete the reversals of the invest(2,3, 8,6, l). b) What invest with elements from the firm 1,2,n has the most reversals How numerous does it accept? (c) What is the totaliance betwixt the exoteric interval of INSERTION SORT (discern topic 2) and the ns in the input invest? Justify your counter-argument. (d) Suppose we are comparing implementations of implantation nature and cohere nature (a past past natureing algorithm, which we earn glean encircling after in the semester) on the similar agent. Control inputs of extent n, implantation nature operates in 8n2 steps, date cohere nature operates in 64n log2n steps. Control which values of n does implantation nature whack cohere nature?

    3. (25 points) Let A[1..] be an invest of n disjoined bulk. If i A, the brace (i,j) is determined an reversal of A. (a) List complete the reversals of the invest(2,3, 8,6, l). b) What invest with elements from the firm 1,2,n has the most reversals How numerous does it accept? (c) What is the totaliance betwixt the exoteric interval of INSERTION SORT (discern topic 2) and the ns in the input invest? Justify your counter-argument. (d) Suppose we are comparing implementations of implantation nature and cohere nature (a past past natureing algorithm, which we earn glean encircling after in the semester) on the similar agent. Control inputs of extent n, implantation nature operates in 8n2 steps, date cohere nature operates in 64n log2n steps. Control which values of n does implantation nature whack cohere nature?

    Expert Counter-argument

     

    Hi,
    Inversions are defined as when i<j then a[i]>a[j]
    dedicated invest is a={2,3,8,6,1}
    hence reversals are

    2,1 
    3,1 
    8,6 
    8,1 
    6,1 
    b. the most reversals appear when the invest is in descending adjust, i.e { n,n-1...1}
    now, the reversals earn be any span elements fine from it which is n(n-1)/2
    c.Implantation nature basically reduces the reversals by 1 in each interation, hence compute of reversals is instantly proportional to the interval of implantation nature,
    so hence interval of reversal nature is dedicated by O(n+f(n)) where f(n) is compute of reversals, 
    hence if compute of reversals are n then implantation natures operate in O(n) yet if its n^2, then it operates in O(n^2)
    here we want to apprehend control complete n where 
    

    8n2 < 64nlogn

    =n < 8logn

    =n/8 < logn

    i.e n-8logn>0

    on solving the level we gain, n=43
    hence control n<=43, implantation nature is better