Posts

Showing posts with the label algorithm

Merge Sort Implementation in Python

Today I post the python implementation of merge sort. This is straight-forward implementation of the algorithm presented in the book Introduction to Algorithms [CLRS]. If you think the implementation can be more Pythonic, feel free to comment. Here is my code: def merge(li, start, mid, end): left_li = [] left_li.extend(li[start : mid+1]) right_li = [] right_li.extend(li[mid+1: end+1]) left_li.append(2147483647) right_li.append(2147483647) i = 0 j = 0 for k in range(start, end+1): if left_li[i] You can test the merge sort implementation against this problem: http://www.spoj.com/problems/MERGSORT/

insertion sort implementation in python

I am working on my Python coding skills and this time reviewing various algorithms with which I have not been in touch for many days. So I start with sorting, here is my code for insertion sort: def insertion_sort(ara): for j in range(1, len(ara)): key = ara[j] i = j - 1 while i >= 0 and key More python implementation of various algorithms are coming soon. Meanwhile if you feel that there is chance to improve my code, or make it more pythonic, please feel free to comment. Oh, if you are not familiar with algorithms yet and wording what the ... is insertion sort, please visit the wikipedia article on insertion sort .