bubble_02.pptx

Download Report

Transcript bubble_02.pptx

Bubble Sort
The basics of a popular sorting algorithm
How it works
It
is a simple sorting algorithm that works by
repeatedly stepping through the list to be
sorted, comparing each pair of adjacent items
and swapping them if they are in the wrong
order.
The algorithm starts at the beginning of the
data set.
It compares the first two elements, and if the
first is greater than the second, it swaps them.
Continued…
The
pass through the list is repeated until no
swaps are needed, which indicates that the list
is sorted.
 The algorithm gets its name from the way
smaller elements "bubble" to the top of the list.
Continued…
Because
it only uses comparisons to operate on
elements, it is a comparison sort.
Although bubble sort is one of the simplest
sorting algorithms to understand and
implement, its O(n2) complexity means that its
efficiency decreases dramatically on lists of
more than a small number of elements.
Performance
Bubble
sort has worst-case and average
complexity both О(n2), where n is the number
of items being sorted.
There exist many sorting algorithms with
substantially better worst-case or average
complexity of O(n log n). Even other О(n2)
sorting algorithms, such as insertion sort, tend
to have better performance than bubble sort.
Therefore, bubble sort is not a practical sorting
algorithm when n is large.
Continued…
The
only significant advantage that bubble sort
has over most other implementations, even
quicksort, but not insertion sort, is that the
ability to detect that the list is sorted is
efficiently built into the algorithm.
Performance of bubble sort over an already
sorted list (best-case) is O(n).
Continued…
Continued…
The
positions of the elements in bubble sort
will play a large part in determining its
performance.
Large elements at the beginning of the list do
not pose a problem, as they are quickly
swapped. Small elements towards the end,
however, move to the beginning extremely
slowly.
This has led to these types of elements being
named rabbits and turtles.
Examples
Rabbits
and Turtles
http://www.algolist.net/Algorithms/Sorting/Bu
bble_sort
Sources
http://www.cs.duke.edu/~ola/papers/bubble.p
df
https://www.cs.tcd.ie/publications/tech-
reports/reports.05/TCD-CS-2005-57.pdf
http://www.algolist.net/Algorithms/Sorting/Bu
bble_sort