Apply Now Apply Now Apply Now
header_logo
Post thumbnail
PROGRAMMING LANGUAGES

Selection Sort in C: Algorithm, Program, and Examples

By Vishalini Devarajan

Selection Sort is one of the most beginner-friendly sorting algorithms in C programming. It works by repeatedly selecting the smallest element from an unsorted part of an array and placing it in the correct position.

Although it is not the fastest sorting algorithm for large datasets, Selection Sort helps beginners understand sorting logic, array traversal, swapping, and nested loops in C.

In this article, you will learn what is Selection Sort in C, how the algorithm works, its syntax, time complexity, advantages, disadvantages, and how to implement it with a complete C program and examples.

Table of contents


  1. TL;DR
  2. What is Selection Sort in C?
  3. Why is Selection Sort Important?
  4. How Does Selection Sort Work?
    • Pass 1
    • Pass 2
    • Pass 3
  5. Selection Sort Algorithm
  6. C Program for Selection Sort
    • Explanation
  7. Output Example
  8. Dry Run of Selection Sort
    • Initial Array
    • First Iteration
    • Second Iteration
    • Third Iteration
    • Fourth Iteration
  9. Time Complexity of Selection Sort
  10. Advantages of Selection Sort
  11. Disadvantages of Selection Sort
  12. Selection Sort vs Bubble Sort
  13. Ascending vs Descending Order in Selection Sort
    • Ascending Order
    • Descending Order
  14. Applications of Selection Sort
  15. Why Selection Sort is Not Used for Large Applications
  16. Common Mistakes Beginners Make
  17. Conclusion
  18. FAQs
    • What is Selection Sort in C?
    • What is the time complexity of Selection Sort?
    • Is Selection Sort stable?
    • Why is Selection Sort important for beginners?
    • Which is better: Selection Sort or Bubble Sort?

TL;DR

  1. Selection Sort is a simple comparison-based sorting algorithm used to arrange elements in ascending or descending order.
  2. The algorithm repeatedly finds the minimum element and swaps it with the current position.
  3. It uses nested loops and sorts the array in place without needing extra memory.
  4. Selection Sort has a time complexity of O(n²), making it less efficient for large datasets.
  5. It is mainly used for learning sorting basics and understanding algorithm logic in C.

What is Selection Sort in C?

Selection Sort is a sorting algorithm that repeatedly selects the smallest element from the unsorted part of an array and places it at the beginning.

The process continues until all elements are in sorted order.

The algorithm divides the array into two parts:

  1. Sorted portion
  2. Unsorted portion

In every iteration, the smallest element from the unsorted section is selected and swapped with the first unsorted element.

Why is Selection Sort Important?

Selection Sort is one of the first sorting algorithms taught in programming because it helps beginners understand how comparison-based sorting works.

Before learning more complex algorithms like Merge Sort or Quick Sort, students often start with Selection Sort to build confidence in:

  1. Array traversal
  2. Nested loops
  3. Swapping values
  4. Comparison logic
  5. Algorithm thinking

Even though modern applications rarely use Selection Sort for large datasets, understanding its logic creates a strong foundation for learning data structures and algorithms.

Another reason beginners learn Selection Sort is that the algorithm is predictable and easy to dry-run manually.

Unlike some advanced sorting techniques that involve recursion or partitioning logic, Selection Sort follows a direct step-by-step process.

How Does Selection Sort Work?

Selection Sort works through repeated comparisons.

The algorithm scans the array to find the smallest element and swaps it into the correct position.

The steps are:

  1. Start from the first element.
  2. Find the minimum element in the unsorted part.
  3. Swap it with the current element.
  4. Move to the next position.
  5. Repeat until the array is sorted.

Suppose the array is:

64, 25, 12, 22, 11

Pass 1

  1. Smallest element = 11
  2. Swap with 64

11, 25, 12, 22, 64

Pass 2

  1. Smallest element = 12
  2. Swap with 25

11, 12, 25, 22, 64

Pass 3

  1. Smallest element = 22
  2. Swap with 25

11, 12, 22, 25, 64

The array is now sorted.

Selection Sort Algorithm

The basic Selection Sort algorithm is:

  1. Start from index 0
  2. Find the minimum element in the unsorted array
  3. Swap it with the current element
  4. Move to the next index
  5. Repeat until the array is sorted

C Program for Selection Sort

Below is the complete C program for Selection Sort.

include <stdio.h>

int main() {

int arr[] = {64, 25, 12, 22, 11};

int n = 5;

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[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

}

printf(“Sorted array:\n”);

for(i = 0; i < n; i++) {

printf(“%d “, arr[i]);

}

return 0;

}

MDN

Explanation

The outer loop controls the number of passes needed for sorting.

The inner loop searches for the smallest element in the unsorted part of the array.

Once the minimum element is found, swapping is done using a temporary variable.

Finally, the sorted array is displayed using a loop.

Output Example

Sorted array:

11, 12, 22, 25, 64

You can also explore HCL GUVI’s DSA ebook to understand sorting algorithms, arrays, searching techniques, and core programming concepts used in data structures and algorithms.

Dry Run of Selection Sort

Understanding the dry run helps beginners visualize how the algorithm works internally.

Consider the array:

29, 10, 14, 37, 13

Initial Array

29, 10, 14, 37, 13

First Iteration

  1. Smallest element found = 10
  2. Swap 29 and 10

Updated array:

10, 29, 14, 37, 13

Second Iteration

  1. Smallest element in remaining array = 13
  2. Swap 29 and 13

Updated array:

10, 13, 14, 37, 29

Third Iteration

  1. Smallest element already in correct place = 14

Updated array:

10, 13, 14, 37, 29

Fourth Iteration

  1. Smallest element between 37 and 29 = 29
  2. Swap 37 and 29

Final sorted array:

10, 13, 14, 29, 37

This repeated process of selecting the minimum value gives Selection Sort its name.

Time Complexity of Selection Sort

Selection Sort performs comparisons repeatedly regardless of the input arrangement.

CaseTime Complexity
Best CaseO(n²)
Average CaseO(n²)
Worst CaseO(n²)

The space complexity of Selection Sort is O(1) because it sorts the array in place without using extra memory.

Advantages of Selection Sort

Selection Sort offers several benefits for beginners learning algorithms:

  1. Easy to understand and implement
  2. Requires very little extra memory
  3. Performs fewer swaps compared to Bubble Sort
  4. Useful for small datasets
  5. Helps beginners understand sorting fundamentals clearly

Disadvantages of Selection Sort

Despite its simplicity, Selection Sort has limitations:

  1. Inefficient for large datasets
  2. Time complexity remains O(n²)
  3. Slower compared to more advanced sorting algorithms
  4. Not suitable for performance-critical applications

Selection Sort vs Bubble Sort

Selection Sort and Bubble Sort are both basic sorting algorithms, but they work differently.

FeatureSelection SortBubble Sort
Main IdeaSelect the minimum elementSwap adjacent elements
Number of SwapsFewerMore
Time ComplexityO(n²)O(n²)
EfficiencySlightly betterSlightly slower
Beginner FriendlyYesYes

Ascending vs Descending Order in Selection Sort

Selection Sort can arrange elements in both ascending and descending order.

Ascending Order

In ascending order sorting, the algorithm selects the minimum element during each pass.

Example:

11, 12, 22, 25, 64

Descending Order

In descending order sorting, the algorithm selects the maximum element during each pass.

Example:

64, 25, 22, 12, 11

The sorting condition can be easily modified based on the required order.

Applications of Selection Sort

Selection Sort is mainly used in educational and small-scale scenarios.

Some common applications include:

  1. Teaching sorting concepts to beginners
  2. Small datasets with limited memory
  3. Systems where swap operations are costly
  4. Understanding algorithm optimization basics

Why Selection Sort is Not Used for Large Applications

Selection Sort becomes inefficient when the dataset size increases.

For example, if an array contains thousands or millions of elements, the algorithm performs a very large number of comparisons.

Modern applications usually prefer faster sorting algorithms, such as:

  1. Merge Sort
  2. Quick Sort
  3. Heap Sort
  4. Tim Sort

These algorithms perform significantly better for large-scale systems, databases, search engines, and real-time applications.

However, Selection Sort remains valuable for learning core algorithm concepts.

💡 Did You Know?

Selection Sort performs the same number of comparisons regardless of whether the input array is completely unsorted or already sorted. This is because it repeatedly scans the remaining unsorted portion of the array to find the minimum element, resulting in a consistent O(n²) comparison count. However, unlike Bubble Sort, Selection Sort minimizes data movement by performing at most one swap per pass. This characteristic can make it useful in environments where memory write operations are costly or limited. Although rarely used in production systems, Selection Sort introduces foundational concepts such as comparison-based ordering and element positioning, which help learners understand more advanced sorting algorithms and optimization techniques.

Common Mistakes Beginners Make

While implementing Selection Sort in C, beginners often make these mistakes:

  1. Incorrect Loop Range: Using the wrong loop condition may cause array index errors.
  2. Forgetting to Update minIndex: If the minimum index is not updated properly, sorting fails.
  3. Swapping Inside the Inner Loop: Swapping should happen only after finding the minimum element.
  4. Using Wrong Array Size: Incorrect array length can cause unexpected output.

Practicing dry runs step-by-step can help avoid these errors.

Curious about how these concepts work in real-world applications? Join HCL GUVI’s C Programming course to build projects and learn programming concepts used in backend and web development.

Conclusion

Selection Sort in C is one of the easiest sorting algorithms for beginners to grasp.

It introduces important programming concepts such as nested loops, array traversal, comparisons, and swapping.

Although it is not suitable for large-scale applications due to its O(n²) time complexity, it is still a valuable algorithm for learning the basics of sorting and problem-solving in C programming.

By practicing Selection Sort programs and dry-running examples, beginners can build a stronger foundation before moving on to advanced sorting algorithms like Merge Sort and Quick Sort.

FAQs

1. What is Selection Sort in C?

Selection Sort is a simple sorting algorithm that repeatedly selects the minimum element from an unsorted array and places it in the correct position.

2. What is the time complexity of Selection Sort?

The best, average, and worst-case time complexity of Selection Sort is O(n²).

3. Is Selection Sort stable?

No, Selection Sort is generally considered an unstable sorting algorithm because equal elements may change their relative positions.

4. Why is Selection Sort important for beginners?

Selection Sort helps beginners understand sorting logic, nested loops, comparisons, and swapping techniques in programming.

MDN

5. Which is better: Selection Sort or Bubble Sort?

Selection Sort usually performs fewer swaps than Bubble Sort, making it slightly more efficient in some cases.

Success Stories

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Get in Touch
Chat on Whatsapp
Request Callback
Share logo Copy link
Table of contents Table of contents
Table of contents Articles
Close button

  1. TL;DR
  2. What is Selection Sort in C?
  3. Why is Selection Sort Important?
  4. How Does Selection Sort Work?
    • Pass 1
    • Pass 2
    • Pass 3
  5. Selection Sort Algorithm
  6. C Program for Selection Sort
    • Explanation
  7. Output Example
  8. Dry Run of Selection Sort
    • Initial Array
    • First Iteration
    • Second Iteration
    • Third Iteration
    • Fourth Iteration
  9. Time Complexity of Selection Sort
  10. Advantages of Selection Sort
  11. Disadvantages of Selection Sort
  12. Selection Sort vs Bubble Sort
  13. Ascending vs Descending Order in Selection Sort
    • Ascending Order
    • Descending Order
  14. Applications of Selection Sort
  15. Why Selection Sort is Not Used for Large Applications
  16. Common Mistakes Beginners Make
  17. Conclusion
  18. FAQs
    • What is Selection Sort in C?
    • What is the time complexity of Selection Sort?
    • Is Selection Sort stable?
    • Why is Selection Sort important for beginners?
    • Which is better: Selection Sort or Bubble Sort?