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 attire of n plain collection. If i < j and A>A, the brace (i,j) is named an violation of A. (a) List total the violations of the attire(2,3, 8,6, l). b) What attire with elements from the cemal 1,2,n has the most violations How manifold does it own? (c) What is the connection among the present opportunity of INSERTION SORT (discern inquiry 2) and the ns in the input attire? Justify your reply. (d) Suppose we are comparing implementations of implantation class and be-mixed class (a further past classing algorithm, which we accomplish gather encircling following in the semester) on the selfselfsimilar record. Ce inputs of dimension n, implantation class operates in 8n2 steps, term be-mixed class operates in 64n log2n steps. Ce which values of n does implantation class whack be-mixed class?

    3. (25 points) Let A[1..] be an attire of n plain collection. If i A, the brace (i,j) is named an violation of A. (a) List total the violations of the attire(2,3, 8,6, l). b) What attire with elements from the cemal 1,2,n has the most violations How manifold does it own? (c) What is the connection among the present opportunity of INSERTION SORT (discern inquiry 2) and the ns in the input attire? Justify your reply. (d) Suppose we are comparing implementations of implantation class and be-mixed class (a further past classing algorithm, which we accomplish gather encircling following in the semester) on the selfselfsimilar record. Ce inputs of dimension n, implantation class operates in 8n2 steps, term be-mixed class operates in 64n log2n steps. Ce which values of n does implantation class whack be-mixed class?

    Expert Reply

     

    Hi,
    Inversions are defined as when i<j then a[i]>a[j]
    loving attire is a={2,3,8,6,1}
    accordingly violations are

    2,1 
    3,1 
    8,6 
    8,1 
    6,1 
    b. the most violations happen when the attire is in descending manage, i.e { n,n-1...1}
    now, the violations accomplish be any two elements selected from it which is n(n-1)/2
    c.Implantation class basically reduces the violations by 1 in each interation, hence enumerate of violations is quickly proportional to the opportunity of implantation class,
    so accordingly opportunity of violation class is loving by O(n+f(n)) where f(n) is enumerate of violations, 
    hence if enumerate of violations are n then implantation classs operate in O(n) notwithstanding if its n^2, then it operates in O(n^2)
    here we demand to comprehend ce total n where 
    

    8n2 < 64nlogn

    =n < 8logn

    =n/8 < logn

    i.e n-8logn>0

    on solving the coextension we gain, n=43
    accordingly ce n<=43, implantation class is better