What is the Difference Between Bubble Sort and Selection Sort

The main difference between bubble sort and selection sort is that the bubble sort operates by repeatedly swapping the adjacent elements if they are in the wrong order while the selection sort sorts an array by repeatedly finding the minimum element from the unsorted part and placing that at the beginning of the array. 

Sorting is the method of arranging data in an arranged order. It helps to search for data elements quickly. Sorting algorithms are useful in multiple fields such as in machine learning, and big data analysis to manipulate large datasets.  There are various sorting algorithms. Bubble sort and selection sort are two of them.

Key Areas Covered

1. What is Bubble Sort
     – Definition, Functionality
2. What is Selection Sort
     – Definition, Functionality
3. What is the Difference Between Bubble Sort and Selection Sort
     – Comparison of Key Differences

Key Terms

Algorithm, Big Data, Bubble Sort, Machine Learning, Selection Sort

Difference Between Bubble Sort and Selection Sort - Comparison Summary (1)

What is Bubble Sort

Bubble sort is a sorting algorithm, which sorts the elements in increasing order. It repeatedly compares the adjacent items. And, if the item in the left is larger than the item in the right, the items swap.

Main Difference - Bubble Sort vs Selection Sort

An example is as follows.

5  8  1  6  9  2

Consider 5 and 8. It is not necessary to swap the two numbers as 5 1; instead, we swap two items. Now the list is as follows.

5 1 8  6  9  2

Now consider 8 and 6. As 8 > 6, we swap those two numbers. The list is as follows.

5  1  6  8  9  2

Now consider 8 and 9. It is not necessary to swap the numbers as 8 < 9.  Then consider 9 and 2. We should swap the two values as 9 > 2. After completing the first iteration, the list appears as below.

5  1   6  8  2  9

The largest item is in the rightmost position. Now, we only have to consider 5 1  6  9 2. We can compare 5 and 1. As 5 > 1, we swap the values. Then, as before, we can follow the same procedure. The list after completing the iteration is as follows.

1  5  6  2  8  9

Now, 8 and 9 are the largest items in the list, but they are already sorted. Now we have to consider 1 5 6 2. This process continues and finally, we can obtain a sorted list.

What is Selection Sort

Selection sort is a sorting algorithm that sorts the elements in increasing order. After finding the smallest element in the unsorted part of the array, it swaps that element with the first position in the list.

Difference Between Bubble Sort and Selection Sort

An example is as follows.

7  8  5  4  9  2

We take the minimum value as 7. We check the value 8. It is not less than 7. So, we check 5. It is less than 7. Now, the minimum value is 5. Now, consider 4. It is less than the minimum value (5). Therefore, now the minimum value is 4. Next, we consider the number 9.  It is not less than the current minimum value (4). So, we move to the next element, which is 2.  It is less than the current minimum value (4). Now the minimum value is 2. We can swap 7 and 2. Now the list is as follows.

2  8  5  4  9  7

Now, 2 is already sorted, and it is the smallest number in the list. The rest is the unsorted list. We should now sort 8 5 4 9 7. We consider 8 as the minimum value. The value 5 is less than the minimum value (8). So, now the minimum value is 5. Then, value 4 is less than the minimum value. Now the minimum value is 4. Then 9 is not less than minimum value 4. Therefore, we consider the next element 7. It is not less than minimum value 4. Now the minimum is 4. Therefore, we swap the value 4 and value 8 (1st element in the list). Now the list is as follows.

2 4  5  8  9  7

Now, 2 and 4 are sorted. We can sort 5 8 9 7. We consider 5 as the minimum value and repeat the above process and obtain a sorted list at the end.

Difference Between Bubble Sort and Selection Sort

Definition

Bubble sort is a simple sorting algorithm that continuously steps through the list and compares the adjacent pairs to sort the elements. In contrast, selection sort is a sorting algorithm that takes the smallest value (considering ascending order) in the list and moves it to the proper position in the array. Thus, this is the main difference between bubble sort and selection sort.

Functionality

Bubble sort compares the adjacent elements and swap accordingly while selection sort selects the minimum element from the unsorted sub-array and places it at the next position of the sorted subarray.

Efficiency

Furthermore, another difference between bubble sort and selection sort is that the selection sort is efficient compared to the bubble sort.

Speed

Also, speed is another difference between bubble sort and selection sort. Selection sort is faster compared to bubble sort.

Method

Moreover, one other difference between bubble sort and selection sort is that the bubble sort uses item exchanging while selection sort uses item selection.

Conclusion

In summary, the main difference between bubble sort and selection sort is that the bubble sort operates by repeatedly swapping the adjacent elements if they are in the wrong order. In contrast, selection sort sorts an array by repeatedly finding the minimum element from the unsorted part and placing that at the beginning of the array.

Reference:

1. “Bubble Sort.” GeeksforGeeks, 30 Aug. 2018, Available here.
2. “Selection Sort.” GeeksforGeeks, 4 Sept. 2018, Available here.

Image Courtesy:

1. “Bubble-sort-example-300px” By Swfung8 – Own work (CC BY-SA 3.0) via Commons Wikimedia
2. “Selection-Sort-Animation” By Joestape89 (CC BY-SA 3.0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply