# Selection Sort Algorithm

In Selection sort method in each pass we locate smallest number and swap with the last element of sorted list. The selection sort method require (n-1) pass to sort an array.

### Following are the steps that will more clear the concept of Selection Sort.

Pass 1: Find the location ‘loc’ of smallest element in entire array  a i.e. a , a

,a,……a[n-1].

Swap a and a[loc]. Then a is sorted.

Pass 2: Find the location ‘loc’ of smallest element in sub array  a i.e. a , a ,

a,……a[n-1].

Swap a and a[loc]. Then a and a is sorted.

.

.

Pass k: Find the location ‘loc’ of smallest element in sub array  a i.e. a[k+1] , a[k+2] ,

a[k+3],……a[n-1].

Swap a[k] and a[loc]. Then a , a , a,…….,a[k] is sorted.

Pass n-1: Find the location ‘loc’ of smallest elements a[n-2] , a[n-1] .

Swap a[n-2] and a[loc]. Then a , a , a,…….,a[n-1] is sorted.

### Example

To understand the working of selection sort method, consider the following array a with 7 elements Pass 1: loc=location of smallest  element a =4

Interchange elements a and a i.e. 22 and 4 to obtain following array.

Pass 2: Now the location of smallest element from a to a is 5.

Therefor loc = 5

Interchange elements a and a i.e. 36 and 12 to obtain following array.

Pass 3: Now the location of smallest element from a to a is 6.

Therefor loc = 6

Interchange elements a and a i.e. 41 and 15 to obtain following array.

Pass 4: Now the location of smallest element from a to a is 4.

Therefor loc = 4

Interchange elements a and a i.e. 95 and 22 to obtain following array.

Pass 5: Now the location of smallest element from a to a is 5.

Therefor loc = 5

Interchange elements a and a i.e. 95 and 36 to obtain following array.

Pass 6: Now the location of smallest element from a and a is 6.

Therefor loc = 6

Interchange elements a and a i.e. 95 and 41 to obtain following sorted array. ### Complexity Back