diff options
author | Jed Barber <jjbarber@y7mail.com> | 2015-10-14 12:21:23 +1100 |
---|---|---|
committer | Jed Barber <jjbarber@y7mail.com> | 2015-10-14 12:21:23 +1100 |
commit | 3e30658ec5ca3ade4f9295129729127a30b4addf (patch) | |
tree | b18666d29be5e71892f07f084a2b96d731358a1e /quick.adb | |
parent | e8b414fc52cd70dc0d59a8f182eac1d72e56fa6d (diff) |
Added bubble, quick, selection, merge, gnome sorting algorithms
Diffstat (limited to 'quick.adb')
-rw-r--r-- | quick.adb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/quick.adb b/quick.adb new file mode 100644 index 0000000..4586b3b --- /dev/null +++ b/quick.adb @@ -0,0 +1,49 @@ + + +package body Quick is + + + procedure Swap(A, B : in out Element_T) is + Temp : Element_T; + begin + Temp := A; + A := B; + B := Temp; + end Swap; + + + procedure In_Place(Arr : in out Array_T) is + Pivot, Left, Right : Index_T; + begin + if Arr'Length <= 1 then + return; + end if; + + Pivot := Arr'First; + Left := Index_T'Succ(Pivot); + Right := Arr'Last; + + loop + while not (Arr(Left) > Arr(Pivot)) and Left < Arr'Last loop + Left := Index_T'Succ(Left); + end loop; + while Arr(Right) > Arr(Pivot) and Right > Arr'First loop + Right := Index_T'Pred(Right); + end loop; + exit when Left >= Right; + Swap(Arr(Left), Arr(Right)); + end loop; + + Swap(Arr(Pivot), Arr(Right)); + + if Right > Arr'First then + In_Place( Arr(Arr'First .. Index_T'Pred(Right)) ); + end if; + if Right < Arr'Last then + In_Place( Arr(Index_T'Succ(Right) .. Arr'Last) ); + end if; + end In_Place; + + +end Quick; + |