**Explain the algorithm for selection sort.**

*In selection sort the list is divided into two sub-lists sorted and unsorted. These two lists are divided by imaginary wall. We find a smallest element from unsorted sub-list and swap it to the beginning. And the wall moves one element ahead, as the sorted list is increases and unsorted list is decreases. Assume that we have a list on n elements. By applying selection sort, the first element is compared with all remaining (n-1) elements. The smallest element is placed at the first location. Again, the second element is compared with remaining (n-1) elements. At the time of comparison, the smaller element is swapped with larger element. Similarly, entire array is checked for smallest element and then swapping is done accordingly. Here we need n-1 passes or iterations to completely rearrange the data.*

*Algorithm**for selection sort**:*

*selection_sort ( A [ ] , N )*

*Step 1 : Repeat For K = 0 to N – 2 Begin*

*Step 2 : Set POS = K*

*Step 3 : Repeat for J = K + 1 to N – 1 Begin*

*If A[ J ] < A [ POS ]*

*Set POS = J*

*End For*

*Step 5 : Swap A [ K ] with A [ POS ]*

*End For*

*Step 6 : Exit.*

**Thanks**

**Mukesh Rajput**
## No comments:

## Post a Comment

Thanks

Mukesh Rajput