Professional Documents
Culture Documents
A sorting technique puts elements of a list in a certain order. We can put elements in
ascending/descending order. The most-used orders are numerical order and lexicographical
order. Sorting algorithms provide an introduction to a variety of core algorithm concepts,
such as big O notation, divide and conquer algorithms, data structures, best-, worst- and
average-case analysis, time-space trade-offs, and lower bounds.
In this case, two different results are possible, one which maintains the relative order
of records with equal keys, and one which does not:
Take look of few sorting algorithms/techniques in respect to few attributes as per the above
points. Additional computational cost is involved to extend the key comparison to other
components.
Name Average Worst Memory Stable Method
Bubble sort O(n2) O(n2) S(1) Yes Exchanging
Selection sort O(n2) O(n2) S(1) No Selection
Insertion sort O(n2) O(n2) S(1) Yes Insertion
Merge sort O(n log n) O(n log n) S(n) Yes Merging
Quicksort O(n log n) O(n2) S(1) No Partitioning
Heapsort O(n log n) O(n log n) S(1) No Selection
Bubble Sort
The principle behind the development of Bubble Sort algorithm is water bubbles lying at the
bottom of a filled water container. Bubbles lying at bottom of the container being very small
because of water pressing on them. As these bubbles move upward their size gradually
increases because water pressure decreases on them.
Bubble sort is a simple sorting algorithm. It 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 pass through the list is repeated until no swaps are needed, which indicates that the
list is sorted. Because it only uses comparisons to operate on elements, it is a comparison
sort.
For example consider the unsorted list: 22, 11, 99, 44, 33. If we apply the above principle
then what will be position of very first element and sorted list?
Sorted List: 11, 22, 33, 44, 99. This is ascending order list. We consider two adjacent
elements in very first, i.e., 22 and 11. It is found that 22 is greater than 11 means 22>11. It is
required to exchange them then partial sorted list will be as shown in Step 1. Maximum
number of steps required in one round in the sorting is always being equal to unsorted data
size minus 1, e.g., n-1 where n is size of data. For above example we have arrangement like.
Here in this example n=5. Number of steps are 4 for Round 1. After Round 1 unsorted list
will contain n-1 data. After Round 2 unsorted list will contain n-2 data. After Round 3
unsorted list will contain n-3 data. After Round 4 unsorted list will contain n-4 data.
Round -1
Round 2
Round 3
Round 4