Selection Sort:-
If elements being arranged in increasing order with this technique, then smallest element reaches first at its correct position,then second smallest and so on.
Code:
Output:

Explanation:
Step 1: For i=0, x[0] is taken as min value and value of i is taken as loc.
Remaining values are checked with min value,If found less than min then that value is taken as min value and that location is taken as loc.
Finally x[0] is swapped by the element that is found minimum.
Step 2: For i=1, x[1] is taken as min value and value of i is taken as loc.
Remaining values are checked with min value,If found less than min then that value is taken as min value and that location is taken as loc.
Finally x[1] is swapped by the element that is found minimum.
Step 3: For i=2, x[2] is taken as min value and value of i is taken as loc.
Remaining values are checked with min value,If found less than min then that value is taken as min value and that location is taken as loc.
Finally x[2] is swapped by the element that is found minimum.
Step 4: For i=3, x[3] is taken as min value and value of i is taken as loc.
Remaining values are checked with min value,If found less than min then that value is taken as min value and that location is taken as loc.
Finally x[3] is swapped by the element that is found minimum.
Step 5: For i=4, x[4] is taken as min value and value of i is taken as loc.
Remaining values are not left.
Finally x[4] is swapped by the element that is found minimum i.e. with itself.
If elements being arranged in increasing order with this technique, then smallest element reaches first at its correct position,then second smallest and so on.
Code:
#include<stdio.h> void main() { int x[5]={16,15,2,13,6}; int i,j,temp,n=5; int min,loc; printf("\nList of elements"); for(i=0;i<n;i++) printf("\n%d",x[i]); for(i=0;i<n;i++) { min=x[i]; loc=i; for(j=i+1;j<n;j++) { if(x[j]<min) { min=x[j]; loc=j; } } if(loc!=i) { temp=x[i]; x[i]=x[loc]; x[loc]=temp; } } printf("\nAfter Sorting"); for(i=0;i<n;i++) printf("\n%d",x[i]); getch(); }

Explanation:
Step 1: For i=0, x[0] is taken as min value and value of i is taken as loc.
Remaining values are checked with min value,If found less than min then that value is taken as min value and that location is taken as loc.
Finally x[0] is swapped by the element that is found minimum.
Step 2: For i=1, x[1] is taken as min value and value of i is taken as loc.
Remaining values are checked with min value,If found less than min then that value is taken as min value and that location is taken as loc.
Step 3: For i=2, x[2] is taken as min value and value of i is taken as loc.
Remaining values are checked with min value,If found less than min then that value is taken as min value and that location is taken as loc.
Remaining values are checked with min value,If found less than min then that value is taken as min value and that location is taken as loc.
Remaining values are not left.
Finally x[4] is swapped by the element that is found minimum i.e. with itself.





No comments:
Post a Comment