From de18d69e22a84a288ecc983f21a3535045dbfbb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ishak=20Numanagi=C4=87?= <ishak.numanagic@gmail.com> Date: Mon, 24 Jan 2022 04:00:35 +0100 Subject: [PATCH] stdlib/algorithms/insertionsort.codon --- stdlib/algorithms/insertionsort.codon | 38 ++++++++++++++++++--------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/stdlib/algorithms/insertionsort.codon b/stdlib/algorithms/insertionsort.codon index b6202fa5..b5732eca 100644 --- a/stdlib/algorithms/insertionsort.codon +++ b/stdlib/algorithms/insertionsort.codon @@ -1,32 +1,46 @@ -def _insertion_sort[S,T](arr: Array[T], begin: int, end: int, keyf: Callable[[T], S]): +# (c) 2022 Exaloop Inc. All rights reserved. + + +def _insertion_sort( + arr: Array[T], begin: int, end: int, keyf: Callable[[T], S], T: type, S: type +) -> void: i = begin + 1 while i < end: x = arr[i] j = i - 1 while j >= begin and keyf(x) < keyf(arr[j]): - arr[j+1] = arr[j] + arr[j + 1] = arr[j] j -= 1 - arr[j+1] = x + arr[j + 1] = x i += 1 -def insertion_sort_array[S,T](collection: Array[T], size: int, keyf: Callable[[T], S]): + +def insertion_sort_array( + collection: Array[T], size: int, keyf: Callable[[T], S], T: type, S: type +) -> void: """ - Insertion Sort - Sorts the array inplace. + Insertion Sort + Sorts the array inplace. """ _insertion_sort(collection, 0, size, keyf) -def insertion_sort_inplace[S,T](collection: List[T], keyf: Callable[[T], S]): + +def insertion_sort_inplace( + collection: List[T], keyf: Callable[[T], S], T: type, S: type +) -> void: """ - Insertion Sort - Sorts the list inplace. + Insertion Sort + Sorts the list inplace. """ insertion_sort_array(collection.arr, collection.len, keyf) -def insertion_sort[S,T](collection: List[T], keyf: Callable[[T], S]) -> List[T]: + +def insertion_sort( + collection: List[T], keyf: Callable[[T], S], T: type, S: type +) -> List[T]: """ - Insertion Sort - Returns the sorted list. + Insertion Sort + Returns the sorted list. """ newlst = copy(collection) insertion_sort_inplace(newlst, keyf)