From b95fa5b691b4edf4d3f2d93ae27534f97769dbeb Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 12 Apr 2021 12:02:35 +0200 Subject: [PATCH] Add Python doctest to selection_sort.py (#167) * Add Python doctest to selection_sort.py * fix isort * Update selection-sort.py * all(selection_sort(arr) or arr == sorted(arr) for arr in arrays) --- sorting/python/selection-sort.py | 49 ++++++++++++++++---------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/sorting/python/selection-sort.py b/sorting/python/selection-sort.py index bab47060..0d394051 100644 --- a/sorting/python/selection-sort.py +++ b/sorting/python/selection-sort.py @@ -4,34 +4,33 @@ of the list. Repeat for each sub-array. O(n^2) time complexity. ''' -def selectionSort(A): - N = len(A) +from string import ascii_letters - for i in range(N - 1, 0, -1): +arrays = ( + [12, 3, 7, 22, -12, 100, 1], + [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], + [4, 1, 3, 9, 7], + [0, -1.5, 1.5, 1.3, -1.3, -1.01, 1.01], + list(reversed(ascii_letters)), +) + + +def selection_sort(arr): + """ + >>> all(selection_sort(arr) or arr == sorted(arr) for arr in arrays) + True + """ + for i in range(len(arr) - 1, 0, -1): k = 0 - for j in range(1, i + 1): - if A[j] > A[k]: + if arr[j] > arr[k]: k = j - swap(A, k, i) + arr[i], arr[k] = arr[k], arr[i] # swap -def swap(A, k, i): - """ - Helper function for swapping elements of the array. - """ - tmp = A[k] - A[k] = A[i] - A[i] = tmp - - -# A = [12, 3, 7, 22, -12, 100, 1] -# A = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] -# A = [4, 1, 3, 9, 7] -A = [5, 4, 3, 2, 1] - -selectionSort(A) - -print("Sorted array: ") -for ele in A: - print("\t" + str(ele)) \ No newline at end of file +if __name__ == "__main__": + for arr in arrays: + selection_sort(arr) + print("Sorted array: ") + for ele in arr: # type: ignore + print(f"\t{ele}")