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;
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]);
}
``` Inline Feedbacks
##### Previous Post ## Bubble Sort Example In Data Structure

##### Next Post ## Bubble Sort Example In Data Structure

In this example, we will see Bubble sort algorithm with example. In bubble sort we'll compare each element of list to the element that follows it. If the current element is greater than the element at next location, then they are in the wrong order, and we'll swap them.

## Insertion Sort Example In Data Structure

In this example, we will see Insertion sort algorithm with example. An insertion sort is quite simple to understand and simple to implement. An insertion sort visits each element of the array, in turn. As it visits a particular element, it scans the array from the beginning to end to determines where in that segment of the array the current value belongs.

## Sorting Methods In Data Structures

Sorting is the problem of taking an arbitrary permutation of n items and rearranging them into the total order. Sorting algorithms are used in all kinds of applications and are necessary for instance, if we plan to use efficient searching algorithms like Binary Search or Interpolation Search since these require their data to be sorted.
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
0