aboutsummaryrefslogtreecommitdiff
path: root/comparison/Python
diff options
context:
space:
mode:
authorShinya Sato <shinnya.sato@gmail.com>2021-10-24 23:04:45 +0900
committerShinya Sato <shinnya.sato@gmail.com>2021-10-24 23:04:45 +0900
commit787dc13c837f0955fbd85b8f26a05d720d361333 (patch)
treefec3abebb1e867e8b2239d7cf07b4c7d13ce0f81 /comparison/Python
parente607a57dfafb784c4e27da143214afb2ccd07fbf (diff)
downloadinpla-787dc13c837f0955fbd85b8f26a05d720d361333.tar.gz
inpla-787dc13c837f0955fbd85b8f26a05d720d361333.zip
Upload the comparison directory
Diffstat (limited to 'comparison/Python')
-rw-r--r--comparison/Python/Makefile16
-rw-r--r--comparison/Python/ack3-11.py9
-rw-r--r--comparison/Python/bsort-40000.py24
-rw-r--r--comparison/Python/fib38.py8
-rw-r--r--comparison/Python/isort-40000.py24
-rw-r--r--comparison/Python/msort-800000.py56
-rw-r--r--comparison/Python/qsort-800000.py34
7 files changed, 171 insertions, 0 deletions
diff --git a/comparison/Python/Makefile b/comparison/Python/Makefile
new file mode 100644
index 0000000..fb6de82
--- /dev/null
+++ b/comparison/Python/Makefile
@@ -0,0 +1,16 @@
+COMMAND = python3
+#TARGET = ack3-11.py fib38.py bsort-40000.py isort-40000.py qsort-800000.py msort-800000.py
+TARGET = fib38.py bsort-40000.py isort-40000.py qsort-800000.py msort-800000.py
+BENCHMARK = bash ../tools/sptest_scripts.sh
+LOG = log.txt
+
+
+.PHONY: all
+
+all: $(LOG)
+
+$(LOG): $(TARGET)
+ $(BENCHMARK) $(COMMAND) '$(TARGET)'
+
+clean:
+ rm -f $(LOG) *~
diff --git a/comparison/Python/ack3-11.py b/comparison/Python/ack3-11.py
new file mode 100644
index 0000000..3e831ac
--- /dev/null
+++ b/comparison/Python/ack3-11.py
@@ -0,0 +1,9 @@
+def ack(m,n):
+ if m==0:
+ return n+1
+ elif n==0:
+ return ack(m-1, 1)
+ else:
+ return ack(m-1, ack(m, n-1))
+
+print(ack(3,11))
diff --git a/comparison/Python/bsort-40000.py b/comparison/Python/bsort-40000.py
new file mode 100644
index 0000000..25ea4c7
--- /dev/null
+++ b/comparison/Python/bsort-40000.py
@@ -0,0 +1,24 @@
+# http://www.geekviewpoint.com/python/sorting/bubblesort
+import random
+
+def bubblesort(A):
+ for i in range(len(A)):
+ for k in range(len(A)-1, i, -1):
+ if (A[k] < A[k-1]):
+ tmp = A[k]
+ A[k] = A[k-1]
+ A[k-1] = tmp
+
+
+def mkRandList(n):
+ a=[]
+ for i in range(1,n+1):
+ a.insert(0, random.randint(0,10000))
+ return a
+
+
+
+a=mkRandList(40000)
+bubblesort(a)
+
+#print(a)
diff --git a/comparison/Python/fib38.py b/comparison/Python/fib38.py
new file mode 100644
index 0000000..efa5380
--- /dev/null
+++ b/comparison/Python/fib38.py
@@ -0,0 +1,8 @@
+def fib(n):
+ if n == 0:
+ return 0
+ elif n == 1:
+ return 1
+ else:
+ return fib(n-1) + fib(n-2)
+print(fib(38))
diff --git a/comparison/Python/isort-40000.py b/comparison/Python/isort-40000.py
new file mode 100644
index 0000000..6fbadb7
--- /dev/null
+++ b/comparison/Python/isort-40000.py
@@ -0,0 +1,24 @@
+import random
+
+def insert_sort(arr):
+ for i in range(1, len(arr)):
+ j = i - 1
+ ele = arr[i]
+ while arr[j] > ele and j >= 0:
+ arr[j + 1] = arr[j]
+ j -= 1
+ arr[j + 1] = ele
+ return arr
+
+
+def mkRandList(n):
+ a=[]
+ for i in range(1,n+1):
+ a.insert(0, random.randint(0,10000))
+ return a
+
+a = mkRandList(40000)
+insert_sort(a)
+#print(a)
+
+
diff --git a/comparison/Python/msort-800000.py b/comparison/Python/msort-800000.py
new file mode 100644
index 0000000..f8b8bd1
--- /dev/null
+++ b/comparison/Python/msort-800000.py
@@ -0,0 +1,56 @@
+# Python program for implementation of MergeSort
+# https://www.geeksforgeeks.org/merge-sort/
+import random
+
+def mergeSort(arr):
+ if len(arr) > 1:
+
+ # Finding the mid of the array
+ mid = len(arr)//2
+
+ # Dividing the array elements
+ L = arr[:mid]
+
+ # into 2 halves
+ R = arr[mid:]
+
+ # Sorting the first half
+ mergeSort(L)
+
+ # Sorting the second half
+ mergeSort(R)
+
+ i = j = k = 0
+
+ # Copy data to temp arrays L[] and R[]
+ while i < len(L) and j < len(R):
+ if L[i] < R[j]:
+ arr[k] = L[i]
+ i += 1
+ else:
+ arr[k] = R[j]
+ j += 1
+ k += 1
+
+ # Checking if any element was left
+ while i < len(L):
+ arr[k] = L[i]
+ i += 1
+ k += 1
+
+ while j < len(R):
+ arr[k] = R[j]
+ j += 1
+ k += 1
+
+
+def mkRandList ( n ):
+ a=[]
+ for i in range(1,n+1):
+ a.insert(0, random.randint(0,10000))
+ return a
+
+a = mkRandList(800000)
+#a = mkRandList(8)
+mergeSort(a)
+#print(a)
diff --git a/comparison/Python/qsort-800000.py b/comparison/Python/qsort-800000.py
new file mode 100644
index 0000000..9a75977
--- /dev/null
+++ b/comparison/Python/qsort-800000.py
@@ -0,0 +1,34 @@
+# Quicksort
+import random
+
+# https://stackoverflow.com/questions/23767787/python-implementation-of-quicksort-fails-to-sort-the-entire-array
+def quickSort(arr):
+ less = []
+ pivotList = []
+ more = []
+ if len(arr) <= 1:
+ return arr
+ else:
+ pivot = arr[0]
+ for i in arr:
+ if i < pivot:
+ less.append(i)
+ elif i > pivot:
+ more.append(i)
+ else:
+ pivotList.append(i)
+
+ less = quickSort(less)
+ more = quickSort(more)
+ return less + pivotList + more
+
+def mkRandList ( n ):
+ a=[]
+ for i in range(1,n+1):
+ a.insert(0, random.randint(0,10000))
+ return a
+
+a = mkRandList(800000)
+#a = mkRandList(8)
+b = quickSort(a)
+#print(a)