diff -r e5c10fcd24001302bb477f4e4660829f7655580d -r 948d1bf1793110219055a2011186f92b7d1833f5 python/algorithms/Algorithm.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/algorithms/Algorithm.py Wed Feb 20 23:29:16 2013 -0600 @@ -0,0 +1,14 @@ +class Algorithm(): + _container = [] + + def initContainer(self, arr): + self._container = arr + + def getContainer(self): + return self._container + + def __str__(self): + return "[ " + " ".join([str(i) for i in self._container]) + " ]" + + def __repr__(self): + return str(self) \ No newline at end of file diff -r e5c10fcd24001302bb477f4e4660829f7655580d -r 948d1bf1793110219055a2011186f92b7d1833f5 python/algorithms/SortAlgorithm.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/algorithms/SortAlgorithm.py Wed Feb 20 23:29:16 2013 -0600 @@ -0,0 +1,68 @@ +from Algorithm import Algorithm + +class SortAlgorithm(Algorithm): + # virtual void Sort + + def Sort(self): + raise Exception("Not implemented!") + +class QuickSort(SortAlgorithm): + + def Sort(self): + self.quicksort(0, len(self._container) - 1) + + + def quicksort(self, left, right): + if right <= left: + return; + + store = 0 + pivotindex = left + right / 2 + self._container[pivotindex], self._container[right] = self._container[right], self._container[pivotindex] + store = left + for i in range(left, right): + if self._container[i] <= self._container[right]: + self._container[i], self._container[store] = self._container[store], self._container[i] + store += 1 + + self._container[store], self._container[right] = self._container[right], self._container[store] + self.quicksort(left, store - 1) + self.quicksort(store + 1, right) + +class MergeSort(SortAlgorithm): + + def Sort(self): + self._container = self.mergesort(self._container) + + def mergesort(self, arr): + if len(arr) <= 1: + return arr + + middle = len(arr) / 2 + left = arr[0:middle] + right = arr[middle:] + + left = self.mergesort(left) + right = self.mergesort(right) + + return self.merge(left, right) + + def merge(self, left, right): + res = [] + + while len(left) > 0 or len(right) > 0: + if len(left) > 0 and len(right) > 0: + if left[0] <= right[0]: + res.append(left[0]) + left = left[1:] + else: + res.append(right[0]) + right = right[1:] + elif len(left) > 0: + res.append(left[0]) + left = left[1:] + elif len(right) > 0: + res.append(right[0]) + right = right[1:] + + return res \ No newline at end of file diff -r e5c10fcd24001302bb477f4e4660829f7655580d -r 948d1bf1793110219055a2011186f92b7d1833f5 python/algorithms/algorithms.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/algorithms/algorithms.py Wed Feb 20 23:29:16 2013 -0600 @@ -0,0 +1,7 @@ +from SortAlgorithm import * + +sort1 = QuickSort() +sort1.initContainer(["test1","test2","tres343","asdf"]) +sort1.Sort() +print str(sort1) +#print('Hello World') diff -r e5c10fcd24001302bb477f4e4660829f7655580d -r 948d1bf1793110219055a2011186f92b7d1833f5 python/algorithms/algorithms.sln --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/algorithms/algorithms.sln Wed Feb 20 23:29:16 2013 -0600 @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "algorithms", "algorithms.pyproj", "{0F06DB39-4775-4DBE-9825-D9582369F01B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0F06DB39-4775-4DBE-9825-D9582369F01B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0F06DB39-4775-4DBE-9825-D9582369F01B}.Release|Any CPU.ActiveCfg = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal