From a7a0c2924c68c7dc457debb60f44bffc4aa80682 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Thu, 15 Oct 2015 21:18:48 +1100 Subject: Added cocktail, insertion, strand sorts --- cocktail.adb | 45 +++++++++++++++++++++++++++++++++++++++++++++ cocktail.ads | 16 ++++++++++++++++ insertion.adb | 29 +++++++++++++++++++++++++++++ insertion.ads | 16 ++++++++++++++++ strandsort.hs | 25 +++++++++++++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 cocktail.adb create mode 100644 cocktail.ads create mode 100644 insertion.adb create mode 100644 insertion.ads create mode 100644 strandsort.hs diff --git a/cocktail.adb b/cocktail.adb new file mode 100644 index 0000000..ecc9e82 --- /dev/null +++ b/cocktail.adb @@ -0,0 +1,45 @@ + + +package body Cocktail is + + + procedure Swap(A, B : in out Element_T) is + Temp : Element_T; + begin + Temp := A; + A := B; + B := Temp; + end Swap; + + + procedure Sort(Arr : in out Array) is + Swapped : Boolean; + begin + if Arr'Length <= 1 then + return; + end if; + + loop + Swapped := False; + for I in Index_T range Arr'First .. Index_T'Pred(Arr'Last) loop + if Arr(I) > Arr(Index_T'Succ(I)) then + Swap( Arr(I), Arr(Index_T'Succ(I)) ); + Swapped := True; + end if; + end loop; + exit when not Swapped; + + Swapped := False; + for I in Index_T reverse range Index_T'Succ(Arr'First) .. Arr'Last loop + if Arr(Index_T'Pred(I)) > Arr(I) then + Swap( Arr(Index_T'Pred(I)), Arr(I) ); + Swapped := True; + end if; + end loop; + exit when not Swapped; + end loop; + end Sort; + + +end Cocktail; + diff --git a/cocktail.ads b/cocktail.ads new file mode 100644 index 0000000..8f21ebb --- /dev/null +++ b/cocktail.ads @@ -0,0 +1,16 @@ + + +generic + + type Index_T is (<>); + type Element_T is private; + type Array_T is array (Index_T range <>) of Element_T; + + with function ">"(X, Y : in Element_T) return Boolean is <>; + +package Cocktail is + + procedure Sort(Arr : in out Array_T); + +end Cocktail; + diff --git a/insertion.adb b/insertion.adb new file mode 100644 index 0000000..863fb8e --- /dev/null +++ b/insertion.adb @@ -0,0 +1,29 @@ + + +package body Insertion is + + + procedure Sort(Arr : in out Array_T) is + Place : Index_T; + Temp : Element_T; + begin + if Arr'Length <= 1 then + return; + end if; + + for I in Index_T range Index_T'Succ(Arr'First) .. Arr'Last loop + if Arr(Index_T'Pred(I)) > Arr(I) then + Place := I; + while Place /= Arr'First and then Arr(Index_T'Pred(Place)) > Arr(I) loop + Place := Index_T'Pred(Place); + end loop; + Temp := Arr(I); + Arr(Index_T'Succ(Place) .. I) := Arr(Place .. Index_T'Pred(I)); + Arr(Place) := Temp; + end if; + end loop; + end Sort; + + +end Insertion; + diff --git a/insertion.ads b/insertion.ads new file mode 100644 index 0000000..19b6b2c --- /dev/null +++ b/insertion.ads @@ -0,0 +1,16 @@ + + +generic + + type Index_T is (<>); + type Element_T is private; + type Array_T is array (Index_T range <>) of Element_T; + + with function ">"(X, Y : in Element_T) return Boolean is <>; + +package Insertion is + + procedure Sort(Arr : in out Array_T); + +end Insertion; + diff --git a/strandsort.hs b/strandsort.hs new file mode 100644 index 0000000..8226b79 --- /dev/null +++ b/strandsort.hs @@ -0,0 +1,25 @@ + + + +strandSort :: Ord a => [a] -> [a] +strandSort list = doStrandSort list [] [] [] + + + +doStrandSort :: Ord a => [a] -> [a] -> [a] -> [a] -> [a] +doStrandSort [] [] [] result = result +doStrandSort [] sublist unsorted result = doStrandSort unsorted [] [] (merge (reverse sublist) result) +doStrandSort input [] unsorted result = doStrandSort (tail input) [head input] unsorted result +doStrandSort input sublist unsorted result = + if (head input) >= (head sublist) + then doStrandSort (tail input) ((head input):sublist) unsorted result + else doStrandSort (tail input) sublist ((head input):unsorted) result + + + +merge :: Ord a => [a] -> [a] -> [a] +merge [] y = y +merge x [] = x +merge (x:xs) (y:ys) = if x <= y then x:(merge xs (y:ys)) else y:(merge (x:xs) ys) + + -- cgit