algorithms

algorithms Commit Details


Date:2013-02-20 23:29:00 (11 years 10 months ago)
Author:Natalie Adams
Branch:default
Commit:948d1bf17931
Parents: e5c10fcd2400
Message:Adding python code

Changes:
Apython/algorithms/Algorithm.py (full)
Apython/algorithms/SortAlgorithm.py (full)
Apython/algorithms/algorithms.py (full)
Apython/algorithms/algorithms.sln (full)

File differences

python/algorithms/Algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
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)
python/algorithms/SortAlgorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
python/algorithms/algorithms.py
1
2
3
4
5
6
7
from SortAlgorithm import *
sort1 = QuickSort()
sort1.initContainer(["test1","test2","tres343","asdf"])
sort1.Sort()
print str(sort1)
#print('Hello World')
python/algorithms/algorithms.sln
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.40891s using 14 queries.