Bubble Sort
Overview of the bubble sort algorithm.
{ Bubble Sort
Bubble Sort is an elementary sorting algorithm. It is so inefficient that
it should never be used in practice. What makes it inefficient is the
excessive number of exchanges that it performs. Much time is wasted by
needlessly moving data in memory. Good alternatives, when seeking a simple,
easy-to-implement sorting algorithm, for a relatively small number of
values, include: Selection Sort, Insertion Sort, and Shell Sort.
A good way to begin thinking about sorting algorithms is to ask: How can we
check whether an array, a[ ], is already sorted in increasing order? One
easy way would be to scan the array, checking that adjacent pairs were in
the proper order. As soon as we found a single pair out of order, we could
stop, and report the error. On the other hand, if we manage to scan through
the whole array and each pair was in order, then the entire array must be
sorted. Here's a Pascal function that does exactly that.
-----------------------------------------------------------------}
function is_sorted : boolean;
var
ok : boolean;
i : integer;
begin
i := 1;
repeat
ok := a[i+1] >= a ;
i := i+1;
until i = N or not ok;
is_sorted := ok;
end;
{ -----------------------------------------------------------------
Verifying that an array is already sorted requires just N-1 comparisons
(and fewer if we find that it isn't sorted.) With a few modifications, this
function can transformed into a sorting procedure that works by
interchanging any out-of-order pairs that are found.
Bubble Sort works by repeatedly scanning the array, checking adjacent pairs
of values to see if they are in the proper order. In the implementation
below, the boolean value ok is used to indicate whether the array is "ok",
meaning "in sorted order". Whenever a pair of values is found to be out of
order, they are interchanged and ok is set to "false", to indicate the
array must be scanned again. This procedure will eventually end when the
array is scanned and all adjacent values are found to be in their proper
sorted order.
Here is a procedure that implements Bubble Sort, assuming a global array a[
] with n elements, and a procedure called swap( ).
-----------------------------------------------------------------}
procedure bubble_sort;
var
ok : boolean;
i : integer;
begin
repeat
ok := true;
for i := 1 to n-1 do
if a > a[i+1] then
begin
swap(a,a[i+1]);
ok := false
end
until ok
end;
{ -----------------------------------------------------------------
This implementation of Bubble Sort requires about N2 comparisons and about
N2 exchanges in the worst case, which occurs when the input data are sorted
in reverse order. The best case scenario for this implementation occurs
when the input data are already sorted in the proper order. In this case,
one pass over the array (with just N comparisons and 0 exchanges) confirms
the array is sorted. The amount of work (comparisons/exchanges) done by
Bubble Sort in average case is difficult to analyze, but it is quite close
to the work needed in the worst case.
---------------------------------------------------------------------------}
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Unknown
New World Interactive: How IMS Game Server Operations Help Enable Greater Cost Management
Discover how New World Interactive migrated to Improbable Multiplayer Services and cut their monthly recurring server o…
GameDev.net
How to update your game without impacting player experience (or revenue)
How much is a minute worth? What about thirty? When it comes to free-to-play titles that rely on micro-transactions and…
GameDev.net
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
Discussion