DIFFERENCE BETWEEN BUBBLE SORT AND SELECTION SORT Difference

DIFFERENCE BETWEEN BUBBLE SORT AND SELECTION SORT

Difference Between Bubble Sort and Selection Sort � Sorting is one of the major task in computer programs in which the elements of an array are arranged in some particular order. Sorting makes searching easier. Bubble sort and Selection sort are the sorting algorithms which can be differentiated through the methods they use for sorting. Bubble sort essentially exchanges the elements whereas selection sort performs the sorting by selecting the element.

Difference Between Bubble Sort and Selection Sort � Another considerable difference between the two is that bubble sort is stable algorithm while selection sort is an unstable algorithm. An algorithm is considered to be steady the elements with the same key occurring in the same order as they were occurring before sorting in the list or array. Generally, most stable and fast algorithms use additional memory.

Bubble Sort Vs Sélection Sort � � Comparison Chart Definition Key Differences Conclusion

Comparison Chart BUBBLE SORT BASIS FOR COMPARISON SELECTION SORT Basic Adjacent element is compared and swapped Largest element is selected and swapped with the last element (in case of ascending order). Best case time complexity O(n) O(n 2) Efficiency Inefficient Improved efficiency as compared to bubble sort Stable Yes No Method Exchanging Selection Speed Slow Fast as compared to bubble sort

Definition of Bubble Sort � � Bubble sort is the simplest iterative algorithm operates by comparing each item or element with the item next to it and swapping them if needed. In simple words, it compares the first and second element of the list and swaps it unless they are out of specific order. Similarly, Second and third element are compared and swapped, and this comparing and swapping go on to the end of the list. The number of comparisons in the first iteration are n-1 where n is the number elements in an array. The largest element would be at nth position after the first iteration. And after each iteration, the number of comparisons decreases and at last iteration only one comparison takes place.

Procedure

Selection Sort � � This algorithm is the slowest sorting algorithm. The best case complexity (When the list is in order) of the Bubble sort is of order n (O(n)), and worst case complexity is O(n 2). In the best case, it is of order n because it just compares the elements and doesn’t swap them. This technique also requires additional space to store the temporary variable. In selection sort, the sorted and unsorted array doesn’t make any difference and consumes an order of n 2 (O(n 2)) in both best and worst case complexity. Selection sort is faster than Bubble sort.

Selection Sort

Key Differences Between Bubble Sort and Selection Sort 1: In the bubble sort, each element and its adjacent element is compared and swapped if required. On the other hand, selection sort works by selecting the element and swapping that particular element with the last element. The selected element could be largest or smallest depending on the order i. e. , ascending or descending. 2: The worst case complexity is same in both the algorithms, i. e. , O(n 2), but best complexity is different. Bubble sort takes an order of n time whereas selection sort consumes an order of n 2 time. 3: Bubble sort is a stable algorithm, in contrast, selection sort is unstable. 4: Selection sort algorithm is fast and efficient as compared to bubble sort which is very slow and inefficient.

Conclusion � Bubble sort algorithm is considered to be the most simple and inefficient algorithm, but selection sort algorithm is efficient as compared to bubble sort. Bubble sort also consumes additional space for storing temporary variable and needs more swaps.
- Slides: 11