Data Structure Bubble Sort
Data Structure Bubble Sort
Data Structure Bubble Sort
Algorithm
Bubble sort is a simple sorting algorithm. This sorting algorithm is
comparison-based algorithm in which each pair of adjacent
elements is compared and the elements are swapped if they are
not in order. This algorithm is not suitable for large data sets as
its average and worst case complexity are of Ο(n 2) where n is the
number of items.
Bubble sort starts with very first two elements, comparing them
to check which one is greater.
We know then that 10 is smaller 35. Hence they are not sorted.
Notice that after each iteration, at least one value moves at the
end.
Algorithm
We assume list is an array of n elements. We further assume
that swap function swaps the values of the given array elements.
begin BubbleSort(list)
return list
end BubbleSort
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of
array element unless the whole array is completely sorted in an
ascending order. This may cause a few complexity issues like
what if the array needs no more swapping as all the elements are
already ascending.
loop = list.count;
end for
end for
Implementation
One more issue we did not address in our original algorithm and
its improvised pseudocode, is that, after every iteration the
highest values settles down at the end of the array. Hence, the
next iteration need not include already sorted elements. For this
purpose, in our implementation, we restrict the inner loop to
avoid already sorted values.