Comparison among Bubble Sort, Selection Sort and Insertion Sort
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(N^{2}) ] . O(N^{2}) swaps.
- Average Case: [ O(N^{2}) ] . O(N^{2}) 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 ( Arr[I], Arr[min_index] ); // Swap I-th smallest element with current I-th place element } }
Time Complexity:
- Best Case [ O(N^{2}) ]. Also O(N) swaps.
- Worst Case: Reversely sorted, and when inner loop makes maximum comparison. [ O(N^{2}) ] . Also O(N) swaps.
- Average Case: [ O(N^{2}) ] . 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.
- Best case complexity is of O(N) while the array is already sorted.
- Number of swaps reduced. O(N) swaps in all cases.
- In-Place sort.
Disadvantage:
- Time complexity in all cases is O(N^{2}), 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(N^{2}) ] . And O(N^{2}) swaps.
- Average Case: [ O(N^{2}) ] . And O(N^{2}) 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 Algorithm | Time Complexity | Space Complexity | ||
---|---|---|---|---|
Best Case | Average Case | Worst Case | Worst Case | |
Bubble Sort | O(N) | O(N^{2}) | O(N^{2}) | O(1) |
Selection Sort | O(N^{2}) | O(N^{2}) | O(N^{2}) | O(1) |
Insertion Sort | O(N) | O(N^{2}) | O(N^{2}) | O(1) |
Recommended Posts:
- Program to sort an array of strings using Selection Sort
- Insertion sort to sort even and odd positioned elements in different orders
- Bubble Sort
- C++ Program for Bubble Sort
- Bubble sort using two Stacks
- Recursive Bubble Sort
- C Program for Bubble Sort
- Sorting Strings using Bubble Sort
- C++ Program for Recursive Bubble Sort
- Java Program for Bubble Sort
- Python Program for Bubble Sort
- Selection Sort
- Sorting Algorithms Visualization : Bubble Sort
- Bubble Sort On Doubly Linked List
- Java Program for Recursive Bubble Sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.