# 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:**

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**.

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**swaps.^{2})**Average Case**: [**O(N**] .^{2})**O(N**swaps.^{2})

** 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(N)**. - 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(N**]. And^{2})**O(1)**swaps.**Worst Case**: Reversely sorted, and when the inner loop makes a maximum comparison. [**O(N**] . Also,^{2})**O(N)**swaps.**Average Case**: [**O(N**] . Also^{2})**O(N)**swaps.

** Space Complexity**: [ auxiliary,

**O(1)**]. In-Place sort.(When elements are shifted instead of being swapped (i.e. temp=a[min], then shifting elements from ar[i] to ar[min-1] one place up and then putting a[i]=temp). If swapping is opted for, the algorithm is not In-place.)

** 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(N**, no best case scenario.^{2})

### 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**] . And^{2})**O(N**swaps.^{2})**Average Case**: [**O(N**] . And^{2})**O(N**swaps.^{2})

** 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) |