Selection Sort in Design and Analysis of Algorithms (DAA)
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.
Working Principle of Selection Sort
Selection Sort follows the steps given below:
- Assume the first unsorted element is the minimum.
- Compare it with every remaining element.
- Find the smallest element.
- Swap it with the first unsorted element.
- Increase the sorted portion by one element.
- 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.
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.
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
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
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 |
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
- Explain the Selection Sort algorithm with a suitable example.
- Write the algorithm and analyze the time complexity of Selection Sort.
- Differentiate between Selection Sort and Bubble Sort.
- Why is Selection Sort called an in-place sorting algorithm?
- Perform Selection Sort on a given array.
- Explain the Best, Average and Worst Case complexities of Selection Sort.
- Why is Selection Sort not considered a stable sorting algorithm?
Viva Questions
- What is Selection Sort?
- Why is it called Selection Sort?
- Is Selection Sort an in-place sorting algorithm?
- Is Selection Sort stable?
- What is the Best Case Time Complexity?
- What is the Worst Case Time Complexity?
- How many passes are required for n elements?
- What is the maximum number of swaps?
- Why is Selection Sort not adaptive?
- Which sorting algorithm performs fewer swaps—Bubble Sort or Selection Sort?
Interview Questions
- Explain the working of Selection Sort.
- What is the difference between Selection Sort and Insertion Sort?
- Why is Selection Sort inefficient for large datasets?
- Can Selection Sort be made stable? Explain.
- Where is Selection Sort practically used?
- What are the advantages of Selection Sort over Bubble Sort?
- Which sorting algorithm would you choose for nearly sorted data?
- What is the space complexity of Selection Sort?
- How many comparisons are performed in Selection Sort?
- 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
- Sort the array 45, 12, 89, 34, 23 using Selection Sort.
- Write the Selection Sort algorithm in C.
- Explain the dry run of Selection Sort using an example.
- Differentiate Selection Sort and Bubble Sort.
- Derive the time complexity of Selection Sort.
- Explain why Selection Sort is not adaptive.
- Write a Java program for Selection Sort.
- 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.