— my journey as a worker bee in quant finance

The Merge Sort — Python Code

I have just begun working on a MOOC on algorithms offered by Stanford. Since this course gives us the liberty to choose a programming language, there isn’t any code discussed in those lectures. I plan to convert any algorithm discussed in those lectures into Python code. Since Merge Sort was the first algorithm discussed, I’m starting with that.

Visualizing the algorithm can be done in 2 stages — first, the recursive splitting of the arrays, 2 each 2 at a time, and second, the merge operation.

Here’s the Python code to merge sort an array.

We can divide a list in half log2n times where n is the length of the list. The second process is the merge. Each item in the list will eventually be processed and placed on the sorted list. So the merge operation which results in a list of size n requiresn operations. The result of this analysis is that log2n splits, each of which costs n for a total of nlog2n operations.

I see you don’t monetize your site, don’t waste your
traffic, you can earn additional bucks every month because you’ve
got high quality content. If you want to know how to make extra $$$,
search for: Boorfe’s tips best adsense alternative