**Selection sort in data structure** is techniques to sort the elements into a particular order.

## Selection sort algorithm

- In this technique, we compare the first element within number with the second number if it is small then we save these number otherwise not.
- Then again the first element is compared with the 3rd element if it is less we swap first element and the third element.
- Again we test the first element with the fourth element and so on.
- This is pass 1 for sorting after this past we will get the smallest number at the top.
- In this pass 2, we compare a second element with the third element, if it is less we perform sorting else not. Then the second compare with fourth and so on.
- This process continues until we get a sorted list.

**Selection sort require passes for n numberis n -1.**

Time complexity of selection sort for worst case** O(n).**

## Selection sort example

Consider following array of 5 number and perform selection sort.

**Sorted array** – 10 20 50 60 70

Passes required for sorting this array **N-1 = 5-1 = 4. **As we have 5 number passes are 4

## Advantages of selection sort

- It works very well with a small number of an array(with minimum data).
- selection sort doesn’t require any additional space while implementing.

## Disadvantages of selection sort

- Not as efficient while working with a large set of data.
- It requires
**n**passes of n element.^{2}

## Selection sort program in c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> int main() { int array[100], n, c, d, position, swap; printf("Enter number of elements\n"); scanf("%d", &n); //Enter range of array printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); // Enter values in array for ( c = 0 ; c < ( n - 1 ) ; c++ ) //code for selection sort { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; }} printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; } |

**Output**

__Related Post__

- Heap Sort
- Quick sort
- Tree in data structure
- Tree traversal in data structure
- Binary Search Tree
- B tree

## Leave a Reply