Anonymous Anonymous - 1 year ago
460 0

Insertion Sort Algorithm implemented in python with timer function. Reads search list from file.


Insertion Sort

import time
from functools import wraps

def fn_timer(function):
    def function_timer(*args, **kwargs):
        t0 = time.clock()
        result = function(*args, **kwargs)
        t1 = time.clock()
        print (t1-t0)
        return result
    return function_timer

def sortCheck(subject):
    if subject == sorted(subject):
        print "List already sorted"

def insertionSort(subject):
    for index in range(1,len(subject)):
        value = subject[index]
        position = index

        while position>0 and subject[position-1]>value:
            subject[position] = subject[position -1]
            position = position -1

fileName = raw_input("Enter filename to search: ")
content = [line.rstrip('\n') for line in open(fileName)]
content = map(int, content)

print len(content)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download