Skip to content
DATA STRUCTURE
DATA STRUCTURE

Learn everything about Data structures.

  • Algorithms
  • DSA
  • Array
  • Stack
  • Queue
  • Matrix
  • Programs
DATA STRUCTURE

Learn everything about Data structures.

Selection Sorting in C Programming

YASH PAL, October 5, 2022March 7, 2023

SELECTION SORTING: The selection sort starts from the first element and searches the entire list until it finds the minimum value. The sort places the minimum value i the first place, select the second element, and searches for the second smallest element. The process continues until the complete list is sorted. A selection sort is one in which successive elements are selected in order and placed into their proper sorted positions. The selection process needs to be done only from 1 to n – 1 rather than up to n. Analysis of the selection is so straightforward.

[br]

The first pass makes – 1 comparison, the second pass makes n – 2, and so on So total comparisons are (N – 1) + (N2) (NE -3) ++ 2 + 1 = N (N – 1) / 2 = 0 (N³) The little additional storage required (except to hold few temporary variables.). The sort may therefore be categorized as O (N³), although it is faster than the bubble sort there is no improvement if the input file is completely sorted or unsorted. Despite the fact that it is simple to code, it is unlikely that the straight selection sort would be used on any files but those for which n is small.

[br]

The selection sort algorithm for sorting works as follows. First, find the smallest element in the list and put it in the first position. Then find the second smaller element in the list and put it in the second position. It is to be noted that the number of comparisons in the selection sort algorithm is independent of the original sort of the elements. The selection sort method requires (n – 1) passes to sort an array.

[br]

Algorithm – Selection sorting in C

SELECTION SORT (A, N)

Here A is an array with N elements. This algorithm sorts the array A with N elements in ascending order.

1. Repeat Steps 2 to 5 for I = 1 to N – 1:
2. Set MIN : = A [U]
3. Set POS : = I.
4. Repeat Steps for J = 1 + 1 to N:
       If A [J] < MIN, then :
            (a) Set MIN : = A [J].
            (b) Set POS : = J.
       [End of If structure.]
    [End of Step 4 loop.]
5. If I != POS, then
         (a) Set TEMP : = A [ I ]
         (b) Set A [ 1 ] = A [ POS ]
         (c) Set A [ POS ] : = TEMP
    [End of If structure.]
    [End of step 1 loop.]
6. Return

[br]

 

Selection Sorting (First Logic) program in c.

#include<stdio.h>
#include<conio.h>
#define SIZE 10
void selection_sort(int [], int);
void main()
{
    int a[SIZE],i,n;
    clrscr();
    printf("Enter how many elements");
    scanf("%d",&n);

    /* Input Array */

    for(i=0;i<n;i++)
    {
        printf("Enter element %d",i+1);
        scanf("%d",&a[i]);
    }

    selection_sort(a,n);

    /* Output Array */

    for(i=0;i<n;i++)
        printf("%d ", a[i]);

    getch();
}

void selection_sort(int a[], int n)
{
    int i,j,t,min,minpos;

    for(i=0;i<n-1;i++)
    {
        min = a[i];
        minpos = i;

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

        if(i != minpos)
        {
            t = a[i];
            a[i] = a[minpos];
            a[minpos] = t;
        }
    }
}

[br]

 

Selection Sorting (Second Logic) program in c.

#include<stdio.h>
#include<conio.h>
#define SIZE 10
void selection_sort(int [], int);
void main()
{
    int a[SIZE],i,n;
    clrscr();
    printf("Enter how many elements");
    scanf("%d",&n);

    /* Input Array */

    for(i=0;i<n;i++)
    {
        printf("Enter element %d",i+1);
        scanf("%d",&a[i]);
    }

    selection_sort(a,n);

    /* Output Array */

    for(i=0;i<n;i++)
        printf("%d ", a[i]);

    getch();
}

void selection_sort(int a[], int n)
{
    int minpos;
    int i,j,t;

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

        for(j=i+1;j<n-1;j++)
        {
            minpos = i;
            for(j=i+1;j<n;j++)
            {
                if(a[minpos] > a[j])
                minpos = j;
            }
        }

        if(minpos != i)
        {
            t = a[i];
            a[i] = a[minpos];
            a[minpos] = t;
        }
    }
}

[br]

Also, Read
  1. Sorting Techniques in Data Structures
  2. Bubble Sorting in C Programming
  3. Insertion Sorting in C Programming
  4. Shell Sorting in C Programming
  5. Merge Sorting in C Programming
  6. Radix Sorting in C Programming
  7. Quick Sorting in C Programming
  8. Heap Sorting in C Programming
  9. Selection Sorting Algorithm
selection sorting Sorting techniques DSAselection sortingsorting techniques

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

  • HackerRank Dynamic Array Solution in Python
  • HackerRank 2D Array DS solution in Python
  • HackeRank Array – DS solution in Python
  • Streaming Demystified: How Much Data Does Your Favorite Show Really Use?
  • Parenthesis Matching program in C programming
  • HackerRank Dynamic Array Solution in Python
  • HackerRank 2D Array DS solution in Python
  • HackeRank Array – DS solution in Python
  • Streaming Demystified: How Much Data Does Your Favorite Show Really Use?
  • Parenthesis Matching program in C programming
  • About US
  • Contact US
  • Data Structures and algorithms tutorials
  • Digital Communication Tutorials
  • DMCA
  • HackerRank All Algorithms problems solutions
  • HackerRank C problems solutions
  • HackerRank C++ problems solutions
  • HackerRank Java solutions
  • HackerRank Python solutions
  • Human Values Tutorials
  • Internet of Things Tutorials
  • Privacy Policy
©2025 DATA STRUCTURE | WordPress Theme by SuperbThemes