Difference Between Bubble Sort and Selection Sort


The task of arranging elements of an array in a particular order is referred to as sorting. The sorting of an array or a list is mainly done to make the searching easier. There are two types of sorting algorithms namely, Bubble Sort and Selection Sort.

Bubble sort performs sorting of data by exchanging the elements, while the selection sort performs sorting of data by selecting the elements.

Read this article to learn more about bubble sort and selection sort and how these two sorting techniques are different from each other.

What is Bubble Sort?

Bubble sort is a simple sorting algorithm. Bubble sort iterates through a list and compares adjacent pairs of elements to sort them. It swaps the elements of an array based on the adjacent elements.

The major advantage of bubble sort is that it is more efficient in comparison to selection sort. However, it is slower in comparison to selection sort.

Bubble sort uses item exchanging to swap elements. Therefore, the elements are repeatedly swapped in bubble sorting until all the elements are in the right order.

Following is the Bubble Sort Algorithm

begin BubbleSort(list)
   for all elements of list
      if list[i] > list[i+1]
	     swap(list[i], list[i+1])
      end if
   end for
return list
end BubbleSort

What is Selection Sort?

Selection sort is a sorting algorithm which takes either the minimum value (ascending order) or the maximum value (descending order) in the list and places it at the proper position. The minimum or the maximum number from the list is obtained first. Next, it selects the minimum or maximum element from unsorted sub-array and puts it in the next position of the sorted sub-array, and so on. Selection sort is considered as an unstable sorting algorithm.

Selection sort algorithm is relatively more efficient in comparison to bubble sort. However, the number of comparisons made during iterations is more than the element swapping that is done. Also, in selection sort, the location of every element in a list is previously known. This means the user only searches for the element that needs to be inserted at a specific position. Selection sort is quick in comparison to bubble sort and it uses item selection for sorting of elements.

Following is the Selection Sort Algorithm

  • Step 1 − Set MIN to location 0.

  • Step 2 − Search the minimum element in the list.

  • Step 3 − Swap with value at location MIN.

  • Step 4 − Increment MIN to point to next element.

  • Step 5 − Repeat until list is sorted.

Now, let us discuss the differences between bubble sort and selection sort in detail.

Difference between Bubble Sort and Selection Sort

The following are the important differences between bubble sort and selection sort −

S.No.

Bubble Sort

Selection Sort

1.

Bubble sort is a simple sorting algorithm which continuously moves through the list and compares the adjacent pairs for proper sorting of the elements.

Selection sort is a sorting algorithm which takes either smallest value (ascending order) or largest value (descending order) in the list and place it at the proper position in the list.

2.

Bubble sort compares the adjacent elements and move accordingly.

Selection sort selects the smallest element from the unsorted list and moves it at the next position of the sorted list.

3.

Bubble sort performs a large number of swaps or moves to sort the list.

Selection sort performs comparatively less number of swaps or moves to sort the list.

4.

Bubble sort is relatively slower.

Selection sort is faster as compared to bubble sort.

5.

The efficiency of the bubble sort is less.

The efficiency of the selection sort is high.

6.

Bubble sort performs sorting of an array by exchanging elements.

Selection sort performs sorting of a list by the selection of element.

Conclusion

The most significant difference that you should note here is that bubble sort uses item exchanging to swap elements, while selection sort uses item selection for sorting.

Updated on: 20-Feb-2023

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements