Introduction to Algorithms No.5

Hello. Long time not to see. I enjoyed summer vacation...

This is the merge-sort algorithm:

https://github.com/uriha421/introduction_to_algorithms/blob/master/I_Foundations/merge_sort.c


Merge-sort is one of divide-and-conquer algorithms. It may be important to understand the concept. Let me explain it.
Divide means cutting a problem into some small ones. Conquer means solving the divided problems. Then we combine the divided problems and we solve the entire problem. In merge-sort, divide is:
<1 2 8 7 6 3 5 4>   --->   <1 2 8 7> <6 3 5 4>
 Conquer is:
<1 2 8 7> <6 3 5 4>   --->   <1 2 7 8> <3 4 5 6>
Combine is:
<1 2 7 8> <3 4 5 6>   --->   <1 2 3 4 5 6 7 8>
Now let T(n) be the running time of sorting a n-size array. Then, we have the following recursion:
T(n) = c, (if n = 1), 2T(n/2) + O(n), (if n > 1).
(2T(n/2) is the running time of conquer. O(n) is the running time of combine here.)

In conclusion, we can get a recursion from a divide-conquer algorithm. And we can calculate the running time.


Thank you. See you tomorrow.
 

Comments

Popular posts from this blog

Introduction to Algorithms No.3

Introduction to Algorithms No.7

Introduction to Algorithms No.10