Skip to content

Instantly share code, notes, and snippets.

@ma7dev
Created April 25, 2022 10:30
Show Gist options
  • Save ma7dev/76496e16d04abfa603b1b405c9057a8c to your computer and use it in GitHub Desktop.
Save ma7dev/76496e16d04abfa603b1b405c9057a8c to your computer and use it in GitHub Desktop.
Testing sorted()
import random
def naive_way_of_sorting(arr):
for i in range(len(arr)):
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[i]
return arr
def test_sorting(size):
arr = [random.randint(0, size) for _ in range(size)]
# naive implementation that I know forsure it works
expected_sorted_arr = naive_way_of_sorting(arr)
# the targeted implementation to be compared to
actual_sorted_arr = sorted(arr)
# comparing expectation vs. actual value
assert expected_sorted_arr == actual_sorted_arr
test_cases = [0, 1, 10, 100, 1000, 10000]
for size in test_cases:
print('Testing sorting for size:', size)
test_sorting(size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment