summaryrefslogtreecommitdiff
path: root/sort/bubble.adb
diff options
context:
space:
mode:
Diffstat (limited to 'sort/bubble.adb')
-rw-r--r--sort/bubble.adb64
1 files changed, 64 insertions, 0 deletions
diff --git a/sort/bubble.adb b/sort/bubble.adb
new file mode 100644
index 0000000..f4f2485
--- /dev/null
+++ b/sort/bubble.adb
@@ -0,0 +1,64 @@
+
+
+package body Bubble 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_T) 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;
+ end loop;
+
+ end Sort;
+
+
+ procedure Optimized(Arr : in out Array_T) is
+ N, NewN : Index_T;
+ begin
+ if Arr'Length <= 1 then
+ return;
+ end if;
+
+ N := Arr'Last;
+ loop
+ NewN := Arr'First;
+
+ for I in Index_T range Arr'First .. Index_T'Pred(N) loop
+ if Arr(I) > Arr(Index_T'Succ(I)) then
+ Swap( Arr(I), Arr(Index_T'Succ(I)) );
+ NewN := I;
+ end if;
+ end loop;
+
+ N := NewN;
+ exit when N = Arr'First;
+ end loop;
+
+ end Optimized;
+
+
+end Bubble;
+