-
[Introduction to Algorithms, CLRS] Exercises 2.3-4알고리즘 2018. 2. 10. 18:38
This is a solution for Introduction to Algorithms. I write this for my study purpose.
Exercises 2.3-4
We can express insertion sort as a recursive procedure as follows. In order to sort A[1 . . n] , we recursively sort A[1 . . n] and then insert A[n] into the sorted array A[1 . . n-1] . Write a recurrence for the running time of this recursive version of insertion sort.
Answer:
There are two steps in this recursive sorting algorithm:
1. Sort the sub-array A[1..n−1]
2. Insert A[n] into the sorted sub-array from step 1 in proper position
For n=1, step 1 doesn’t take any time as the sub-array is an empty array and step 2 takes constant time, i.e. the algorithm runs in Θ(1) time.
For n>1, step 1 again calls for the recursion for n−1 and step 2 runs in Θ(n) time.
So, we can write the recurrence as:
'알고리즘' 카테고리의 다른 글
[Introduction to Algorithms, CLRS] Exercises 2.3-6 (0) 2018.04.20 [Introduction to Algorithms, CLRS] Exercises 2.3-5 (0) 2018.04.13 [Introduction to Algorithms, CLRS] Exercises 2.3-3 (0) 2018.01.27 [Introduction to Algorithms, CLRS] Exercises 2.3-2 (0) 2017.12.30 [Introduction to Algorithms, CLRS] Exercises 2.3-1 (0) 2017.12.30