Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The answers posted for this question did not provide a complete analysis of the cost for the subquestions. Would be much appreciated if the work
The answers posted for this question did not provide a complete analysis of the cost for the subquestions. Would be much appreciated if the work is shown for each solution. Thanks.
Problem 1. We would like to take into account memory hierarchies when analyzing Insertion Sort. Assume that your machine takes time fi) to access memory location i, for some nondecreasing function f). Most of the time, when doing algorithmic analysis, we assume f(i) is a constant. In reality it is a slow growing function. For this problem, to keep things simple, we will assume that fi) grows linearly, which is pretty drastic. Also, to keep things simple, we will count the time for comparisons. To compare two elements in locations i and j it takes time, i + j. We will modify Insertion Sort to take the memory access cost into account. The array to be sorted is in A[1,...,n]. To insert the ith element of A into its proper location, put it into A[O], where it is cheap to access. Then compare the elements of A to this value starting at location 1 and going up, rather than starting at location i - 1 and going down. (Note that if we were going to be really efficient, we could move an element into location 1 before comparing it to the element being inserted, so the cost would be just 0 + 1 = 1. This is not in the spirit of the problem. If you must do this, then, to be fair, you should also keep track of the cost of moving items.) (a) Write the pseudo-code for this modified version of Insertion Sort. Do not use a sentinel. (b) What input gives the best-case cost? (c) Analyze the exact best-case cost. Show your work. (d) What input gives the worst case cost? (e) Analyze the exact worst-case cost. Show your work. (f) Analyze the average-case cost for n= 3 (assume distinct values). Show your work. Problem 1. We would like to take into account memory hierarchies when analyzing Insertion Sort. Assume that your machine takes time fi) to access memory location i, for some nondecreasing function f). Most of the time, when doing algorithmic analysis, we assume f(i) is a constant. In reality it is a slow growing function. For this problem, to keep things simple, we will assume that fi) grows linearly, which is pretty drastic. Also, to keep things simple, we will count the time for comparisons. To compare two elements in locations i and j it takes time, i + j. We will modify Insertion Sort to take the memory access cost into account. The array to be sorted is in A[1,...,n]. To insert the ith element of A into its proper location, put it into A[O], where it is cheap to access. Then compare the elements of A to this value starting at location 1 and going up, rather than starting at location i - 1 and going down. (Note that if we were going to be really efficient, we could move an element into location 1 before comparing it to the element being inserted, so the cost would be just 0 + 1 = 1. This is not in the spirit of the problem. If you must do this, then, to be fair, you should also keep track of the cost of moving items.) (a) Write the pseudo-code for this modified version of Insertion Sort. Do not use a sentinel. (b) What input gives the best-case cost? (c) Analyze the exact best-case cost. Show your work. (d) What input gives the worst case cost? (e) Analyze the exact worst-case cost. Show your work. (f) Analyze the average-case cost for n= 3 (assume distinct values). Show your workStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started