You are on page 1of 4

Bubble sort Algorithm

Definition
Bubble sort is a sorting algorithm that works by perpetually stepping through
lists that need to be sorted, comparing each pair of adjacent items and
swapping them if they are in the erroneous order. This passing procedure is
reiterated until no swaps are required, betokening that the list is sorted.
Bubble sort gets its name because more minuscule elements bubble toward
the top of the list. Or an alternate way of putting the most astronomically
immense element at the highest index in the array utilizes an algorithm
called bubble sort. While this method is neither as efficient, nor as
straightforward, as cull sort, it is popularly used to illustrate sorting. We
include it here as an alternate method.
Like cull sort, the conception of bubble sort is to perpetually move the most
sizably voluminous element to the highest index position of the array. As in
cull sort, each iteration reduces the efficacious size of the array. The two
algorithms differ in how this is done. Rather than probe the entire efficacious
array to find the most astronomically immense element, bubble sort fixates
on successive adjacent pairs of elements in the array, compares them, and
either swaps them or not. In either case, after such a step, the more sizably
voluminous of the two elements will be in the higher index position. The
focus then pergrinates to the next higher position, and the process is
reiterated. When the focus reaches the terminus of the efficacious array, the
most sizably voluminous element will have ``bubbled'' from whatever its
pristine position to the highest index position in the efficacious array.

Explanation
The simplest sorting algorithm is bubble sort. The bubble sort works by
iterating down an array to be sorted from the first element to the last,
comparing each pair of elements and switching their positions if
indispensable. This process is reiterated as many times as compulsory, until
the array is sorted. Since the worst case scenario is that the array is in
inversion order, and that the first element in sorted array is the last element
in the commencement array, the most exchanges that will be indispensable
is identically tantamount to the length of the array. Here is a simple example:
Given an array 23154 a bubble sort would lead to the following sequence of
partially sorted arrays: 21354, 21345, 12345. First the 1 and 3 would be

compared and switched, then the 4 and 5. On the next pass, the 1 and 2
would switch, and the array would be in order.
The algorithm for bubble sort requires a pair of nested loops. The outer loop
must iterate once for each element in the data set (of size n) while the inner
loop iterates n times the first time it is entered, n-1 times the second, and so
on. Consider the purport of each loop. As explicated above, bubble sort is
structured so that on each pass through the list the next most immensely
colossal element of the data is peregrinate to its felicitous place. Therefore,
to get all n elements in their correct places, the outer loop must be executed
n times.
The inner loop is executed on each iteration of the outer loop. Its purport is
to put the next most astronomically immense element is being put into
place. The inner loop therefore does the comparing and swapping of adjacent
elements. To determine the intricacy of this loop, we calculate the number of
comparisons that have to be made. On the first iteration of the outer loop,
while endeavoring to place the most astronomically immense element, there
have to be n - 1 comparisons: the first comparison is made between the first
and second elements, the second is made between the second and third
elements, and so on until the n-1th comparison is made between the n-1th
and the nth element. On the second iteration of the outer loop, there is no
desideratum to compare the against the last element of the list, because it
was put in the correct place on the antecedent pass. Therefore, the second
iteration requires only n-2 comparisons. This pattern perpetuates until the
second-to-last iteration of the outer loop when only the first two elements of
the list are unsorted; limpidly in this case, only one comparison is obligatory.
The total number of comparisons, therefore, is (n - 1) + (n - 2)...(2) + (1) =
n(n - 1)/2
The best case for bubble sort occurs when the list is already sorted or
proximately sorted. In the case where the list is already sorted, bubble sort
will terminate after the first iteration, since no swaps were made. Any time
that a pass is made through the list and no swaps were made, it is certain
that the list is sorted. Bubble sort is withal efficient when one desultory
element needs to be sorted into a sorted list, provided that incipient element
is placed at the commencement and not at the cessation. When placed at
the commencement, it will simply bubble up to the correct place, and the
second iteration through the list will engender 0 swaps, ending the sort.
Recall that if the desultory element is placed at the terminus, bubble sort

loses its efficiency because each element more preponderant than it must
bubble all the way up to the top.
The absolute worst case for bubble sort is when the most diminutive element
of the list is at the sizably voluminous end. Because in each iteration only the
most astronomically immense unsorted element gets put in its congruous
location, when the most diminutive element is at the terminus, it will have to
be swapped each time through the list, and it wont get to the front of the list
until all n iterations have occurred. In this worst case, it take n iterations of
n/2 swaps so the order is, again,

n2

You might also like