G P Raja Sekhar Department of Mathematics I I T Kharagpur
Acknowledgements : Ahmad Abdullah, Pronoy Sikdar, Anshul Kamra, Sugam Agrawal & Students of DAA course 2006-07, Data Structures & Algorithms 2006-07.
BUBBLE SORT
The bubble sort is the oldest and simplest sort in use. The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items. This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the beginning of the list.
Pros: 1) Simplicity and ease of implementation. 2) Auxiliary Space used is O (1). Cons: 1) Very inefficient. General complexity is O (n2). Best case complexity is O(n).
Bubble Sort Efficiency
INSERTION SORT
The insertion sort works just like its name suggests - it inserts each item into its proper place in the final list. The simplest implementation of this requires two list structures - the source list and the list into which sorted items are inserted. To save memory, most implementations use an in-place sort that works by moving the current item past the already sorted items and repeatedly swapping it with the preceding item until it is in place.
Pros: Auxiliary space used is O (1). Cons: General Complexity is O (n2). Best Case is O(n) when the list is already sorted.
Insertion Sort Efficiency
HEAP SORT
All elements to be sorted are inserted into a heap, and the heap organizes the elements added to it in such a way that either the largest value (in a max-heap) or the smallest value (in a min-heap) can be quickly extracted. Moreover, because this operation preserves the heap's structure, the largest/smallest value can be repeatedly extracted until none remain. Each time we delete (extract) the maximum, we place it in the last location of the array not yet occupied, and