diff options
| author | Shinya Sato <shinnya.sato@gmail.com> | 2022-05-02 09:50:21 +0900 |
|---|---|---|
| committer | Shinya Sato <shinnya.sato@gmail.com> | 2022-05-02 09:50:21 +0900 |
| commit | df4949f8b08cbe3cfd99933d5bb14a373f47f2dd (patch) | |
| tree | c516c3c45d79a4843114ddd44a83b9ca87250cb7 /comparison/Python | |
| parent | 7ea48cd96551247ecc763a5e42404ffa3464d0b0 (diff) | |
| parent | 779860e35f5c67c7fd48ef876784f87afcfdb321 (diff) | |
| download | inpla-df4949f8b08cbe3cfd99933d5bb14a373f47f2dd.tar.gz inpla-df4949f8b08cbe3cfd99933d5bb14a373f47f2dd.zip | |
Merge branch 'develop'
Diffstat (limited to 'comparison/Python')
| -rw-r--r-- | comparison/Python/Makefile | 3 | ||||
| -rw-r--r-- | comparison/Python/dummy.py | 64 | ||||
| -rw-r--r-- | comparison/Python/msort-260000.py | 64 | ||||
| -rw-r--r-- | comparison/Python/qsort-260000.py | 43 |
4 files changed, 172 insertions, 2 deletions
diff --git a/comparison/Python/Makefile b/comparison/Python/Makefile index fb6de82..21f4b3d 100644 --- a/comparison/Python/Makefile +++ b/comparison/Python/Makefile @@ -1,6 +1,5 @@ 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 +TARGET = dummy.py ack3-11.py fib38.py bsort-40000.py isort-40000.py qsort-260000.py msort-260000.py BENCHMARK = bash ../tools/sptest_scripts.sh LOG = log.txt diff --git a/comparison/Python/dummy.py b/comparison/Python/dummy.py new file mode 100644 index 0000000..98c2206 --- /dev/null +++ b/comparison/Python/dummy.py @@ -0,0 +1,64 @@ +# 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 + + +def validation(alist): + for i in range(len(alist)-1): + if (alist[i] > alist[i+1]): + return False + + return True + + +a = mkRandList(10) +mergeSort(a) +print(validation(a)) diff --git a/comparison/Python/msort-260000.py b/comparison/Python/msort-260000.py new file mode 100644 index 0000000..e7d7011 --- /dev/null +++ b/comparison/Python/msort-260000.py @@ -0,0 +1,64 @@ +# 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 + + +def validation(alist): + for i in range(len(alist)-1): + if (alist[i] > alist[i+1]): + return False + + return True + + +a = mkRandList(260000) +mergeSort(a) +print(validation(a)) diff --git a/comparison/Python/qsort-260000.py b/comparison/Python/qsort-260000.py new file mode 100644 index 0000000..fbca39d --- /dev/null +++ b/comparison/Python/qsort-260000.py @@ -0,0 +1,43 @@ +# 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 + + +def validation(alist): + for i in range(len(alist)-1): + if (alist[i] > alist[i+1]): + return False + + return True + + +a = mkRandList(260000) +b = quickSort(a) +print(validation(b)) |
