Tutorials C Programming Selection Sort Example In Data Structure

Selection Sort Example In Data Structure

In this example we explain selection sort in data structure. A selection sort is slightly more complicated. but relatively easy to understand and to code. It is one of the slow sorting technique. Given an unsorted array of integer values, a selection sort visits each element of the array, in turn. As it visits an element, it employs a second index to scan the array from the present location to the end while it identifies the least (smallest) value in that segment of the array. It then swaps that least value with the current value. Because one index scans the array once while another index scans part of the array each time, this sort algorithm is also O(n2).

Simple steps for selection sort. [Assume we are sorted the n items in array A] for i = 1 to n do

for j = j+1 to n do

if A[i] > A[j] then swap(A[i],A[j])

Example: C program, Function to arrange number in ascending order using Selection sort technique.

#include<stdio.h>
void selection_sort(int a[],int n);
{
    int i,j,pos,small,temp;
    for(i=0;i<n-1;i++)
    {
        small=a[i]; /* Initial small number in ith pass */
        pos=i; /* Position of smaller number */
        /* Find the minimum of remaining elements along with the position */
        for(j=i+1;j<n;j++)
        {
            if(a[i]<small)
            {
                small=a[j];
                pos=j;
            }
        }
        
        /* Exchange ith item with least item */
        temp=a[pos];
        a[pos] = a[i];
        a[i]=temp;
    }
}

void main()
{
    int i,n,a[20];
    printf("Enter the number of elements to sort");
    scanf("%d",&n);
    printf("Enter %d elements to sort ",n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    selection_sort(a,n)
    printf("The sort elements are");
    for(i=0;i<n;i++)
    printf("%d",&a[i]);
}
Total
0
Shares
guest
0 Comments
Inline Feedbacks
View all comments
Previous Post
Data Structure Tutorials

Bubble Sort Example In Data Structure

Next Post
Android Tutorials

How to Use Hindi Font in Android Application

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
0
Would love your thoughts, please comment.x
()
x