

Comparison among Bubble Sort, Selection Sort and Insertion Sort
source link: https://www.geeksforgeeks.org/comparison-among-bubble-sort-selection-sort-and-insertion-sort/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1. Bubble Sort
Bubble sort repeatedly compares and swaps(if needed) adjacent elements in every pass. In i-th pass of Bubble Sort (ascending order), last (i-1) elements are already sorted, and i-th largest element is placed at (N-i)-th position, i.e. i-th last position.
Algorithm:
BubbleSort (Arr, N) // Arr is an array of size N. { For ( I:= 1 to (N-1) ) // N elements => (N-1) pass { // Swap adjacent elements of Arr[1:(N-I)]such that // largest among { Arr[1], Arr[2], ..., Arr[N-I] } reaches to Arr[N-I] For ( J:= 1 to (N-I) ) // Execute the pass { If ( Arr [J] > Arr[J+1] ) Swap( Arr[j], Arr[J+1] ); } } }
Optimization of Algorithm: Check if there happened any swapping operation in the inner loop (pass execution loop) or not. If there is no swapping in any pass, it means the array is now fully sorted, hence no need to continue, stop the sorting operation. So we can optimize the number of passes when the array gets sorted before the completion of all passes. And it can also detect if the given / input array is sorted or not, in the first pass.
BubbleSort (Arr, N) // Arr is an array of size N. { For ( I:= 1 to (N-1) ) // N elements => (N-1) pass { // Swap adjacent elements of Arr[1:(N-I)]such that // largest among { Arr[1], Arr[2], ..., Arr[N-I] } reaches to Arr[N-I] noSwap = true; // Check occurrence of swapping in inner loop For ( J:= 1 to (N-I) ) // Execute the pass { If ( Arr [J] > Arr[J+1] ) { Swap( Arr[j], Arr[J+1] ); noSwap = false; } } If (noSwap) // exit the loop break; } }
Time Complexity:
- Best Case Sorted array as input. Or almost all elements are in proper place. [ O(N) ]. O(1) swaps.
- Worst Case: Reversely sorted / Very few elements are in proper place. [ O(N2) ] . O(N2) swaps.
- Average Case: [ O(N2) ] . O(N2) swaps.
Space Complexity: A temporary variable is used in swapping [ auxiliary, O(1) ]. Hence it is In-Place sort.
Advantage:
- It is the simplest sorting approach.
- Best case complexity is of O(N) [for optimized approach] while the array is sorted.
- Using optimized approach, it can detect already sorted array in first pass with time complexity of O(1).
- Stable sort: does not change the relative order of elements with equal keys.
- In-Place sort.
Disadvantage:
- Bubble sort is comparatively slower algorithm.
2. Selection Sort
Selection sort selects i-th smallest element and places at i-th position. This algorithm divides the array into two parts: sorted (left) and unsorted (right) subarray. It selects the smallest element from unsorted subarray and places in the first position of that subarray (ascending order). It repeatedly selects the next smallest element.
Algorithm:
SelectionSort (Arr, N) // Arr is an array of size N. { For ( I:= 1 to (N-1) ) // N elements => (N-1) pass { // I=N is ignored, Arr[N] is already at proper place. // Arr[1:(I-1)] is sorted subarray, Arr[I:N] is undorted subarray // smallest among { Arr[I], Arr[I+1], Arr[I+2], ..., Arr[N] } is at place min_index min_index = I; For ( J:= I+1 to N ) // Search Unsorted Subarray (Right lalf) { If ( Arr [J] < Arr[min_index] ) min_index = J; // Current minimum } // Swap I-th smallest element with current I-th place element If (min_Index != I) Swap ( Arr[I], Arr[min_index] ); } }
Time Complexity:
- Best Case [ O(N2) ]. And O(1) swaps.
- Worst Case: Reversely sorted, and when the inner loop makes a maximum comparison. [ O(N2) ] . Also, O(N) swaps.
- Average Case: [ O(N2) ] . Also O(N) swaps.
Space Complexity: [ auxiliary, O(1) ]. In-Place sort.
Advantage:
- It can also be used on list structures that make add and remove efficient, such as a linked list. Just remove the smallest element of unsorted part and end at the end of sorted part.
- The number of swaps reduced. O(N) swaps in all cases.
- In-Place sort.
Disadvantage:
- Time complexity in all cases is O(N2), no best case scenario.
3. Insertion Sort
Insertion Sort is a simple comparison based sorting algorithm. It inserts every array element into its proper position. In i-th iteration, previous (i-1) elements (i.e. subarray Arr[1:(i-1)]) are already sorted, and the i-th element (Arr[i]) is inserted into its proper place in the previously sorted subarray.
Find more details in this GFG Link.
Algorithm:
InsertionSort (Arr, N) // Arr is an array of size N. { For ( I:= 2 to N ) // N elements => (N-1) pass { // Pass 1 is trivially sorted, hence not considered // Subarray { Arr[1], Arr[2], ..., Arr[I-I] } is already sorted insert_at = I; // Find suitable position insert_at, for Arr[I] // Move subarray Arr [ insert_at: I-1 ] to one position right item = Arr[I]; J=I-1; While ( J ? 1 && item < Arr[J] ) { Arr[J+1] = Arr[J]; // Move to right // insert_at = J; J--; } insert_at = J+1; // Insert at proper position Arr[insert_at] = item; // Arr[J+1] = item; } } }
Time Complexity:
- Best Case Sorted array as input, [ O(N) ]. And O(1) swaps.
- Worst Case: Reversely sorted, and when inner loop makes maximum comparison, [ O(N2) ] . And O(N2) swaps.
- Average Case: [ O(N2) ] . And O(N2) swaps.
Space Complexity: [ auxiliary, O(1) ]. In-Place sort.
Advantage:
- It can be easily computed.
- Best case complexity is of O(N) while the array is already sorted.
- Number of swaps reduced than bubble sort.
- For smaller values of N, insertion sort performs efficiently like other quadratic sorting algorithms.
- Stable sort.
- Adaptive: total number of steps is reduced for partially sorted array.
- In-Place sort.
Disadvantage:
- It is generally used when the value of N is small. For larger values of N, it is inefficient.
Time and Space Complexity:
Sorting AlgorithmTime ComplexitySpace Complexity Best CaseAverage CaseWorst CaseWorst CaseBubble SortO(N)O(N2)O(N2)O(1)Selection SortO(N2)O(N2)O(N2)O(1)Insertion SortO(N)O(N2)O(N2)O(1)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Recommend
-
11
Insertion sort vs. selection sort (time complexity and performance) yourbasic.org Insertion sor...
-
13
题目¶ 原题地址:https://leetcode.com/problems/insertion-sort-list/
-
8
leetCode解题报告之Insertion Sort List_快乐de胖虎-CSDN博客题目: Sort a linked list using insertion sort. 分析: 这个题目是想要让我们来做一个链表的插入排序问题. 这样,我们只要从...
-
8
Understanding Insertion Sort in Ruby There are lots of ways to sort data. Insertion sort is particularly interesting because it sorts the data in place and is pretty easy...
-
10
How to Write Insertion Sort in Go Jun 24 Originally published at
-
5
Generic Bubble Sort in C# .NET Niels Swimberghe - 8/1/2021 - .NET Follow me on
-
14
Generic Insertion Sort in C# .NET Niels Swimberghe - 8/4/2021 - .NET Follow me on
-
6
Insertion Sort in C# Posted by Code Maze | Updated Date Apr 21, 2022 |
-
4
Time and Space Complexity Analysis of Bubble SortSkip to content
-
3
Time and Space complexity analysis of Selection SortSkip to content
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK