Monday 23 June 2014

                            Selection Sort

    an example of how the algorithm works on a list of 5 integers:




code for Selection Sort:-


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i,j,n,m,*arr,temp=0,min,loc;
    printf("enter the amount of number to be sort\n");
    scanf("%d",&n);
    printf("enter the values to be sort\n");
    arr=(int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
    {
                    scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
                    min=arr[i];
                    for(j=i;j<n;j++)
                    {
                                    if(arr[j]<=min)
                                    {
                                    min=arr[j];
                                    loc=j;
                                    }                
                    }
                    temp=arr[i];
                    arr[i]=min;
                    arr[loc]=temp;
    }
    
     for(i=0;i<n;i++)
     {
                     printf("%d\t",arr[i]);
     }
     system("\n pause");
     return 0;
}
    




No comments:

Post a Comment