FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Selection Sort in Design and Analysis of Algorithms (DAA)




❮ Previous    Next ❯


Selection Sort is one of the simplest comparison-based sorting algorithms used in Design and Analysis of Algorithms (DAA). The algorithm repeatedly finds the smallest element from the unsorted portion of the array and places it at the beginning of the unsorted section. Unlike Bubble Sort, Selection Sort performs only one swap during each pass, thereby reducing the total number of swaps. Although its time complexity is not suitable for large datasets, its simplicity makes it one of the most popular algorithms for learning sorting techniques.


Learning Objectives

After completing this tutorial, you will be able to:

  • Understand the concept of Selection Sort.
  • Explain the working principle of Selection Sort.
  • Write the Selection Sort algorithm and pseudocode.
  • Perform a dry run of the algorithm.
  • Implement Selection Sort in different programming languages.
  • Analyze its time and space complexity.
  • Compare Selection Sort with other sorting algorithms.
  • Solve AKTU university examination and placement questions.

Introduction

Sorting is one of the most fundamental operations in Computer Science. Whether we are arranging student records, employee details, examination marks or product prices, sorting makes searching and data processing much faster. Selection Sort is an elementary sorting algorithm that repeatedly searches for the smallest element from the unsorted portion of an array and places it at its correct position. After every pass, one more element reaches its final sorted position, and the sorted portion of the array gradually increases.


Definition of Selection Sort

Selection Sort is an in-place comparison-based sorting algorithm that divides the array into two parts:

  • Sorted Portion
  • Unsorted Portion

During every iteration, the minimum element from the unsorted portion is selected and exchanged with the first element of that unsorted portion. This process continues until the complete array becomes sorted.


Why is it Called Selection Sort?

The algorithm is known as Selection Sort because during every pass it selects the smallest element from the remaining unsorted elements and places it in its correct sorted position.

Remember

Selection Sort performs many comparisons but only one swap during each pass.


Working Principle of Selection Sort

Selection Sort follows the steps given below:

  1. Assume the first unsorted element is the minimum.
  2. Compare it with every remaining element.
  3. Find the smallest element.
  4. Swap it with the first unsorted element.
  5. Increase the sorted portion by one element.
  6. Repeat until the complete array becomes sorted.

Characteristics of Selection Sort

Property Description
Sorting Technique Comparison Based
Sorting Method In-place Sorting
Stable No
Adaptive No
Extra Memory O(1)
Maximum Swaps n − 1
Suitable For Small datasets

Need of Selection Sort

Selection Sort is useful in situations where swapping elements is more expensive than comparing them. Since the algorithm performs only one swap in each pass, it reduces unnecessary data movement. It is mainly used for educational purposes because of its simplicity and easy implementation.

  • Simple to understand.
  • Easy to implement.
  • Requires only constant extra memory.
  • Performs fewer swaps.
  • Suitable for small datasets.
  • Frequently asked in university examinations.

Algorithm of Selection Sort


SelectionSort(A, n)

for(i = 0; i < n-1; i++)
{
    min = i;

    for(j = i+1; j < n; j++)
    {
        if(A[j] < A[min])
            min = j;
    }

    if(min != i)
        swap(A[i], A[min]);
}

Pseudocode


START

FOR i = 0 TO n-2

    min = i

    FOR j = i+1 TO n-1

        IF A[j] < A[min]

            min = j

    IF min != i

        SWAP(A[i], A[min])

END FOR

STOP

Flowchart of Selection Sort

The following flowchart explains the complete execution process of Selection Sort.

Selection Sort Flowchart

Image Required

  • Start
  • Read Array
  • Initialize i = 0
  • Set Minimum Index
  • Compare Remaining Elements
  • Update Minimum Index
  • Swap Elements
  • Increment i
  • Repeat Until Array Sorted
  • Stop

Basic Example

Consider the following unsorted array:

64    25    12    22    11

During the first pass, the algorithm selects 11 as the minimum element and swaps it with 64. The same process is repeated for the remaining unsorted elements until the complete array becomes sorted.


AKTU Examination Note

Students should remember that Selection Sort always performs (n − 1) passes. After each pass, one element reaches its final sorted position. Questions based on dry run, number of comparisons and time complexity are frequently asked in AKTU semester examinations.


Dry Run of Selection Sort

A Dry Run is the manual execution of an algorithm step by step without using a computer. It helps students understand exactly how Selection Sort works during each iteration.

Consider the following unsorted array.

64    25    12    22    11


Pass 1

The complete array is unsorted. The algorithm searches for the smallest element.

Current Array Minimum Element Swap Array After Pass
64 25 12 22 11 11 64 ↔ 11 11 25 12 22 64

Pass 2

The first element is already sorted. Search only the remaining elements.

Current Array Minimum Element Swap Array After Pass
25 12 22 64 12 25 ↔ 12 11 12 25 22 64

Pass 3

Current Array Minimum Element Swap Array After Pass
25 22 64 22 25 ↔ 22 11 12 22 25 64

Pass 4

Current Array Minimum Element Swap Array After Pass
25 64 25 No Swap Required 11 12 22 25 64

Selection Sort Visualization

Selection Sort Step by Step

Image Required

Create a professional illustration showing each pass of Selection Sort. Highlight:

  • Sorted elements in Green
  • Current minimum element in Red
  • Unsorted elements in Blue
  • Arrow indicating every swap

C Program for Selection Sort


#include 

void selectionSort(int arr[], int n)
{
    int i, j, minIndex, temp;

    for(i = 0; i < n - 1; i++)
    {
        minIndex = i;

        for(j = i + 1; j < n; j++)
        {
            if(arr[j] < arr[minIndex])
            {
                minIndex = j;
            }
        }

        temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }
}

int main()
{
    int arr[] = {64, 25, 12, 22, 11};
    int n = sizeof(arr) / sizeof(arr[0]);

    selectionSort(arr, n);

    printf("Sorted Array:\n");

    for(int i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }

    return 0;
}

C++ Program for Selection Sort


#include 
using namespace std;

void selectionSort(int arr[], int n)
{
    for(int i = 0; i < n - 1; i++)
    {
        int minIndex = i;

        for(int j = i + 1; j < n; j++)
        {
            if(arr[j] < arr[minIndex])
            {
                minIndex = j;
            }
        }

        swap(arr[i], arr[minIndex]);
    }
}

int main()
{
    int arr[] = {64,25,12,22,11};
    int n = sizeof(arr)/sizeof(arr[0]);

    selectionSort(arr,n);

    cout<<"Sorted Array : ";

    for(int i=0;i

Java Program for Selection Sort


public class SelectionSort
{
    static void selectionSort(int arr[])
    {
        int n = arr.length;

        for(int i = 0; i < n - 1; i++)
        {
            int min = i;

            for(int j = i + 1; j < n; j++)
            {
                if(arr[j] < arr[min])
                {
                    min = j;
                }
            }

            int temp = arr[i];
            arr[i] = arr[min];
            arr[min] = temp;
        }
    }

    public static void main(String args[])
    {
        int arr[] = {64,25,12,22,11};

        selectionSort(arr);

        System.out.println("Sorted Array:");

        for(int value : arr)
        {
            System.out.print(value + " ");
        }
    }
}

Python Program for Selection Sort


arr = [64, 25, 12, 22, 11]

n = len(arr)

for i in range(n):

    min_index = i

    for j in range(i + 1, n):

        if arr[j] < arr[min_index]:
            min_index = j

    arr[i], arr[min_index] = arr[min_index], arr[i]

print("Sorted Array:")

for value in arr:
    print(value, end=" ")

PHP Program for Selection Sort


Sorted Array: 11 12 22 25 64 

Observation

Notice that during every pass, the algorithm places exactly one element at its correct sorted position. Even if the array is already sorted, Selection Sort still compares all remaining elements to find the minimum.



Time Complexity Analysis of Selection Sort

Time Complexity is the amount of time taken by an algorithm to execute with respect to the size of the input. Selection Sort always searches the entire unsorted portion of the array to find the smallest element. Therefore, the number of comparisons remains almost the same irrespective of whether the array is already sorted or completely unsorted.

Case Time Complexity
Best Case O(n²)
Average Case O(n²)
Worst Case O(n²)

Why is the Time Complexity O(n²)?

For an array of n elements, Selection Sort performs:

(n − 1) + (n − 2) + (n − 3) + ... + 2 + 1

Total comparisons:

n(n − 1) / 2

Therefore, the overall time complexity is:

O(n²)


Space Complexity

Selection Sort requires only one temporary variable for swapping elements. Therefore, the extra memory required does not depend on the input size.

Parameter Complexity
Auxiliary Space O(1)
In-place Algorithm Yes

Number of Comparisons

Input Size (n) Total Comparisons
5 10
10 45
20 190
50 1225
100 4950

Number of Swaps

Unlike Bubble Sort, Selection Sort performs at most one swap during each pass. Hence, the maximum number of swaps is:

n − 1

Sorting Algorithm Maximum Swaps
Selection Sort n − 1
Bubble Sort O(n²)

Advantages of Selection Sort

  • Simple and easy to understand.
  • Easy to implement.
  • Requires only O(1) extra memory.
  • Performs fewer swaps than Bubble Sort.
  • Suitable for small datasets.
  • Useful for teaching sorting concepts.
  • Works well when memory is limited.

Disadvantages of Selection Sort

  • Time complexity is O(n²).
  • Not suitable for large datasets.
  • Not a stable sorting algorithm.
  • Not adaptive.
  • Performs unnecessary comparisons even for sorted arrays.

Applications of Selection Sort

Although Selection Sort is not commonly used for large-scale applications, it is useful in the following situations.

  • Educational purposes.
  • Sorting small datasets.
  • Embedded systems with limited memory.
  • When swapping is expensive.
  • Interview and competitive programming practice.

Selection Sort vs Bubble Sort

Feature Selection Sort Bubble Sort
Technique Select Minimum Element Swap Adjacent Elements
Best Case O(n²) O(n) (Optimized)
Average Case O(n²) O(n²)
Worst Case O(n²) O(n²)
Maximum Swaps n − 1 Many
Stable No Yes
Adaptive No Yes (Optimized Version)

Selection Sort vs Insertion Sort

Feature Selection Sort Insertion Sort
Method Select Minimum Element Insert into Sorted Portion
Best Case O(n²) O(n)
Worst Case O(n²) O(n²)
Stable No Yes
Adaptive No Yes
Suitable For Small Random Data Nearly Sorted Data

Complexity Summary

Property Value
Best Case Time O(n²)
Average Case Time O(n²)
Worst Case Time O(n²)
Auxiliary Space O(1)
Stable No
Adaptive No
In-place Yes

AKTU Examination Tip

Students should remember the following points for semester examinations:

  • Selection Sort always performs O(n²) comparisons.
  • It performs at most n − 1 swaps.
  • It is an in-place sorting algorithm.
  • It is not stable.
  • It is not adaptive.
  • Auxiliary Space = O(1).


Solved Example

Sort the following array in ascending order using Selection Sort.

29    10    14    37    13

Pass Array After Pass
Initial 29 10 14 37 13
Pass 1 10 29 14 37 13
Pass 2 10 13 14 37 29
Pass 3 10 13 14 37 29
Pass 4 10 13 14 29 37

Final Sorted Array: 10 13 14 29 37


AKTU Previous Year Examination Questions

  1. Explain the Selection Sort algorithm with a suitable example.
  2. Write the algorithm and analyze the time complexity of Selection Sort.
  3. Differentiate between Selection Sort and Bubble Sort.
  4. Why is Selection Sort called an in-place sorting algorithm?
  5. Perform Selection Sort on a given array.
  6. Explain the Best, Average and Worst Case complexities of Selection Sort.
  7. Why is Selection Sort not considered a stable sorting algorithm?

Viva Questions

  1. What is Selection Sort?
  2. Why is it called Selection Sort?
  3. Is Selection Sort an in-place sorting algorithm?
  4. Is Selection Sort stable?
  5. What is the Best Case Time Complexity?
  6. What is the Worst Case Time Complexity?
  7. How many passes are required for n elements?
  8. What is the maximum number of swaps?
  9. Why is Selection Sort not adaptive?
  10. Which sorting algorithm performs fewer swaps—Bubble Sort or Selection Sort?

Interview Questions

  1. Explain the working of Selection Sort.
  2. What is the difference between Selection Sort and Insertion Sort?
  3. Why is Selection Sort inefficient for large datasets?
  4. Can Selection Sort be made stable? Explain.
  5. Where is Selection Sort practically used?
  6. What are the advantages of Selection Sort over Bubble Sort?
  7. Which sorting algorithm would you choose for nearly sorted data?
  8. What is the space complexity of Selection Sort?
  9. How many comparisons are performed in Selection Sort?
  10. Is Selection Sort a divide-and-conquer algorithm? Why?

Multiple Choice Questions (MCQs)

Q. Question
1 Selection Sort belongs to which category?
a) Searching Algorithm
b) Comparison Sorting Algorithm ✅
c) Hashing Algorithm
d) Graph Algorithm
2 The Best Case Time Complexity of Selection Sort is:
a) O(log n)
b) O(n)
c) O(n²) ✅
d) O(n log n)
3 Selection Sort performs at most:
a) n² swaps
b) n swaps
c) n−1 swaps ✅
d) log n swaps
4 Selection Sort requires extra memory of:
a) O(n)
b) O(log n)
c) O(1) ✅
d) O(n²)
5 Selection Sort is:
a) Stable
b) Recursive
c) Not Stable ✅
d) Divide and Conquer
6 Selection Sort is mainly suitable for:
a) Huge databases
b) Small datasets ✅
c) Distributed systems
d) Cloud computing
7 Selection Sort repeatedly selects:
a) Largest element only
b) Random element
c) Smallest element from unsorted portion ✅
d) Middle element
8 Selection Sort is an ______ algorithm.
a) Out-of-place
b) In-place ✅
c) Recursive only
d) Dynamic Programming
9 Total number of passes required for n elements is:
a) n
b) n−1 ✅
c) n²
d) log n
10 The time complexity of Selection Sort in the Average Case is:
a) O(n)
b) O(log n)
c) O(n²) ✅
d) O(n log n)

Practice Questions

  1. Sort the array 45, 12, 89, 34, 23 using Selection Sort.
  2. Write the Selection Sort algorithm in C.
  3. Explain the dry run of Selection Sort using an example.
  4. Differentiate Selection Sort and Bubble Sort.
  5. Derive the time complexity of Selection Sort.
  6. Explain why Selection Sort is not adaptive.
  7. Write a Java program for Selection Sort.
  8. Implement Selection Sort in Python.

Key Takeaways

  • Selection Sort repeatedly selects the smallest element.
  • One element reaches its final position after every pass.
  • It performs O(n²) comparisons.
  • It requires only O(1) auxiliary space.
  • It performs at most (n−1) swaps.
  • It is an in-place sorting algorithm.
  • It is not stable.
  • It is not adaptive.
  • It is suitable for small datasets.
  • It is frequently asked in AKTU examinations and placement interviews.

Summary

Selection Sort is one of the simplest sorting algorithms used in Design and Analysis of Algorithms. The algorithm repeatedly selects the smallest element from the unsorted portion of the array and places it in its correct position. Although its time complexity is O(n²), its simplicity, low memory requirement, and small number of swaps make it an important algorithm for academic learning. Understanding Selection Sort also provides a strong foundation for learning more advanced sorting techniques such as Merge Sort, Quick Sort, and Heap Sort.


Important Points for AKTU Examination

  • Selection Sort is a comparison-based sorting algorithm.
  • Best Case = Average Case = Worst Case = O(n²).
  • Auxiliary Space = O(1).
  • Maximum Swaps = n − 1.
  • Number of Passes = n − 1.
  • Selection Sort is not stable.
  • Selection Sort is not adaptive.
  • Selection Sort is an in-place sorting algorithm.






❮ Previous    Next ❯