Selection Sort Algorithm: Working, Time Complexity & Advantages

0
28
Selection Sort Algorithm
Selection Sort Algorithm

In the world of computer science, Selection Sort algorithm plays a important part where we are talking about comparison based approach and then swap that with other smallest element in the entire group.

Like if i will compare selection sort algorithm with other algorithms like Merge Sort, radix Sort and many more it is easy to implement and efficient. In this blog we will be covering about the selection sort algorithms, complexity analysis, advantages, disadvantages and applications.

Selection sort algorithm

What is a Selection Sort Algorithm?

Selection sort is a simple sorting algorithm that works by repeatedly finding the smallest element from the unsorted part of the list and swapping it with the first unsorted element.

It starts at the beginning of the list and scans through the entire list to find the smallest value and then swaps it with the element in the first position. Then it moves to the next position and repeats the process for the remaining unsorted elements. This continues until all the elements are sorted in order.

Although it is easy to understand and implement selection sort which is not very efficient for large lists because it requires repeatedly scanning through the remaining elements which is making it slower compared to more advanced sorting methods.

How Selection Sort Algorithm Works?

Selection Sort works by dividing the list into two parts which is the sorted part and the unsorted part. The sorted part is empty, and the unsorted part is the entire list.

The algorithm repeatedly finds the smallest element from the unsorted part and swaps it with the first unsorted element which is adding it to the sorted part. This process continues until the entire list is sorted.

Let us say we have a list:
[29, 10, 14, 37, 13]

  1. Find the smallest element → 10
    Swap it with the first element → [10, 29, 14, 37, 13]
  2. Find the smallest in remaining list → 13
    Swap with second element → [10, 13, 14, 37, 29]
  3. Find smallest in remaining → 14
    Already in place → [10, 13, 14, 37, 29]
  4. Find smallest in remaining → 29
    Swap with 37 → [10, 13, 14, 29, 37]

Now the given list of array is sorted.

Implementation of Selection Sort Algorithm

The implementation of selection sort algorithm is explained below in that we have taken array [15, 8, 2, 20, 11, 4] and the easy implementation of code in python programming language.

arr = [15, 8, 2, 20, 11, 4]
n = len(arr)

# Selection Sort
for i in range(n):
    min_idx = i

    for j in range(i + 1, n):
        if arr[j] < arr[min_idx]:
            min_idx = j

    # Swap
    arr[i], arr[min_idx] = arr[min_idx], arr[i]

print("Sorted array:", arr)

Complexity Analysis of Selection Sort Algorithm

This table shows the complexity analysis of selection sort algorithms in every possible way whether it is best case, average case and worst case. These complexities are really helpful when it comes to solve a real problems or requires logic.

TypeComplexity
Best CaseO(n²)
Average CaseO(n²)
Worst CaseO(n²)
Space ComplexityO(1)
Complexity Table of Selection Sort Algorithm

Advantages of Selection Sort

  1. This algorithm is easy to apply when it compared with other algorithms and it is great for the learning purposes.
  2. It requires the constant amount of extra memory i.e, O(1) and not additional storage.
  3. For small datasets its simplicity is more than enough to apply.
  4. It swaps elements when it is necessary because swapping operation is costly to perform.
  5. It only performs comparisons in its initial order.

Disadvantages of Selection Sort

  1. It is not suitable for large datasets because it takes too much time as the number of elements increases.
  2. It always goes through the entire list to find the smallest element even if the list is already sorted.
  3. Its time complexity is always O(n2)O(n^2)O(n2) so it is slower than other efficient algorithms like Merge Sort or Quick Sort.
  4. It is not a stable sorting algorithm which means it may change the relative order of equal elements.
  5. It does not take advantage of the already sorted part of the list, unlike algorithms like Insertion Sort.

Applications of Selection Sort

  1. It is used in teaching and learning to help beginners understand how sorting works step by step.
  2. It is helpful when memory space is very limited since it does not use extra memory.
  3. It can be used in cases where the number of swaps needs to be minimized, like in hardware-related operations.
  4. It is sometimes used in embedded systems where simplicity matters more than speed.
  5. It is suitable for sorting small datasets where performance is not a major concern.

FAQs of Selection Sort Algorithm

What is a Selection Sort?

Selection sort algorithm works on the comparison and swapping based algorithm where we are swapping the current element with the small one through comparison in the whole array.

How does Selection Sort work?

Selection Sort works in the following way:

  • Start with the first element.
  • Find the smallest element in the unsorted part.
  • Swap it with the first element.
  • Repeat the process for the remaining unsorted elements.

What is the time complexity of Selection Sort?

  • Best Case: O(n²)
  • Average Case: O(n²)
  • Worst Case: O(n²)
    It always performs n(n-1)/2 comparisons regardless of the initial order.

What is the space complexity of Selection Sort?

Selection Sort is an in-place algorithm, so it requires O(1) auxiliary space.

Is Selection Sort stable?

No, Selection Sort is not stable by default. Equal elements may not retain their original order after sorting.

Is Selection Sort adaptive?

No, Selection Sort is not adaptive, meaning it does not take advantage of existing order in the array.

When is Selection Sort used?

Selection Sort is mostly used:

  1. In teaching sorting algorithm concepts.
  2. When memory writes are costly since it performs minimal swaps.
  3. For small datasets where performance is not critical.

What are the advantages of Selection Sort?

  1. Simple to understand and implement.
  2. Performs well on small datasets.
  3. Minimizes the number of swaps (at most n-1 swaps).

What are the disadvantages of Selection Sort?

  1. Inefficient for large datasets due to O(n²) time complexity.
  2. Not stable or adaptive.
  3. Does not work well with partially sorted data.

How many swaps and comparisons does Selection Sort make?

Swaps covers O(n) includes at most n-1 swaps and Comparisons covers O(n²) that always makes n(n-1)/2 comparisons.