Matts Scripts - Code
Matt's Scripts live on (only now it is Anna's Scripts)
Matt's Scripts also has a section for allied internet resources such as web hosting, domain name registration, search engine optimisation and submission and webmaster resources. If you have an interesting website in any of the aforementioned industries you may also submit it to iGeneric web scripts directory or scripts section
Who was Matt?
Sport (football and stuff)
(oh, I should probably mention my girlfriend too!)
|Current status||I was at Leeds university studying Computer Science (did you guess?). Presently I am very busy with university coursework/exams etc which unfortunately means that I cannot really afford the time (or money - internet access costs!) to update the site, but we'll see.|
Common Searching and Sorting Algorithms in Python
I implemented some common searching and sorting algorithms in Python to improve my understanding of them in preparation for an algorithms exam. They may not be perfect implementations (unoptimised etc.) but they may help a Python-minded individual understand how they work.
Note: The selection sort and bubble sort algorithms take one argument, a list, and modify it in place as it is a mutable type. The merge sort and quicksort algorithms sorted versions of the provided lists; they don’t modify the lists in place. The search algorithms take two arguments - a list (l), and an element to be found(k). The both return the index of the found element.
def selection_sort(l): n = len(l) for i in xrange(n-1): mini = i for j in xrange(i+1, n): if l[j] < l[mini]: mini = j l[i], l[mini] = l[mini], l[i]
def bubble_sort(l): n = len(l) for i in xrange(n-1): for j in xrange(n-1-i): if l[j] > l[j+1]: l[j], l[j+1] = l[j+1], l[j]
def merge_sort(l): n = len(l) if n > 1: mid = n/2 l1 = merge_sort(l[:mid]) l2 = merge_sort(l[mid:]) return merge(l1, l2) return l def merge(l1, l2): result =  p = len(l1) q = len(l2) i = j = 0 while i < p and j < q: if l1[i] <= l2[j]: result.append(l1[i]) i += 1 else: result.append(l2[j]) j += 1 result.extend(l1[i:]) result.extend(l2[j:]) return result
def quick_sort(l): if l == : return  else: p = l left = quick_sort([x for x in l[1:] if x < p]) right = quick_sort([x for x in l[1:] if x >= p]) return left + [p] + right
def sequential_search(l, k): for i in xrange(len(l)): if l[i] == k: return i
def binary_search(a, k): count = 0 l, r = 0, len(a)-1 while l <= r: count += 1 mid = (l+r)/2 if k == a[mid]: print count return mid else: if k < a[mid]: r = mid else: l = mid+1
Read more MS