Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python test #101

Merged
merged 5 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Python/InsertionSort.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ def insertionSort(arr):
j -= 1
arr[j+1] = key

if __name__=="__main__":
# Driver code to test above
arr = [12, 11, 13, 5, 6]

# Driver code to test above
arr = [12, 11, 13, 5, 6]
# Display original array
print("Original Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")

# Display original array
print("Original Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")
insertionSort(arr)

insertionSort(arr)

# Display sorted array
print(), print("Sorted Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")
# Display sorted array
print(), print("Sorted Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")
8 changes: 5 additions & 3 deletions Python/QuickSort.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def partition(arr,low,high):
# high --> Ending index

# Function to do Quick sort
def quickSort(arr,low,high):
def _quickSort(arr,low,high):
if low < high:

# pi is partitioning index, arr[p] is now
Expand All @@ -30,9 +30,11 @@ def quickSort(arr,low,high):

# Separately sort elements before
# partition and after partition
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)
_quickSort(arr, low, pi-1)
_quickSort(arr, pi+1, high)

def quickSort(arr):
_quickSort(arr, 0, len(arr)-1)


if __name__=="__main__":
Expand Down
8 changes: 8 additions & 0 deletions Python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .BubbleSort import *
from .InsertionSort import *
from .merge_sort import *
from .QuickSort import *
from .radixSort import *
from .selectionSort import *
from .shellSort import *
from .sleepSort import *
10 changes: 5 additions & 5 deletions Python/radixSort.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def radixSort(arr):
counting_sort(arr, exp)
exp *= 10

if __name__=="__main__":
arr = [100, 96, 43, 56, 67, 32, 875, 24342, 7665, 5378755]
radixSort(arr)

arr = [100, 96, 43, 56, 67, 32, 875, 24342, 7665, 5378755]
radixSort(arr)

for i in range(len(arr)):
print(arr[i]),
for i in range(len(arr)):
print(arr[i]),

# code contributed by vibhor agarwal
24 changes: 12 additions & 12 deletions Python/selectionSort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ def selection_sort(array):
array[i], array[min_idx] = array[min_idx], array[i]


if __name__=="__main__":
# Driver code
arr = [64, 25, 12, 22, 11]

# Driver code
arr = [64, 25, 12, 22, 11]
# Display original array
print("Original Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")

# Display original array
print("Original Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")
selection_sort(arr)

selection_sort(arr)

# Display sorted array
print(), print("Sorted Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")
# Display sorted array
print(), print("Sorted Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")
36 changes: 36 additions & 0 deletions Python/shellSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def shell_sort(arr, func=lambda x : x//2):
N = len(arr)
gap = func(N)

# sort the array at specifics gaps until gap is not zero
while gap > 0:

#insertion sort routine
for i in range(gap,N):

temp = arr[i]

j = i
while j >= gap and arr[j-gap] >temp:
arr[j] = arr[j-gap]
j -= gap

arr[j] = temp
gap = func(gap)


if __name__=="__main__":
# Driver code
arr = [64, 25, 12, 22, 11]

# Display original array
print("Original Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")

shell_sort(arr)

# Display sorted array
print(), print("Sorted Array:", end=" ")
for i in range(len(arr)):
print(arr[i], end=" ")
Empty file added test/python_test/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions test/python_test/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
from pathlib import *
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))

from Python import bubbleSort, insertionSort, mergeSort, quickSort, radixSort, selection_sort, shell_sort, sleepSort

sort_key_map = {
'bubbleSort' : bubbleSort,
'insertionSort' : insertionSort,
'mergeSort' : mergeSort,
'quickSort' : quickSort,
'radixSort' : radixSort,
'selection_sort' : selection_sort,
'shell_sort' : shell_sort,
# 'sleepSort' : sleepSort,
}

unsorted_list = [12, 38, 4, 28, 65, 19, 7, 83, 73, 53, 49, 72, 26, 10, 47, 32, 25, 15, 55, 3, 36, 84, 78, 1, 75, 33, 90, 45, 58, 87, 21, 22, 89, 23, 63, 51, 9, 13, 82, 57, 17, 77, 66, 37, 94, 41, 74, 96, 43, 70]
sorted_list = sorted(unsorted_list)
passed = 0

for key in sort_key_map:
try:
arr = unsorted_list.copy()
sort_key_map[key](arr)
if arr == sorted_list:
print(f'{key} passed ✔')
passed += 1
else:
print(f'{key} failed ❌')
except Exception as e:
print(e)
print(f'{key} failed')

print(f'{passed}/{len(sort_key_map)} passed')