This algorithm of selection sort divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. & hence the list is sorted using selection sort .
#include<stdio.h>
#include<conio.h>
void main()
{ int arr[20],n,i,j,temp;
clrscr();
printf("\n\n program to sort array using selection sort");
printf("\n enter no. of elements : ");
scanf("%d",&n);
printf("\n enter array now : \n");
for(i=0;i<n;i++)
{ printf(" enter a[%d] = ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{ for(j=i+1;j<n;j++)
{ if(arr[i]>arr[j])
{ temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("sorted list in ascending order = \n");
for(i=0;i<n;i++)
{ printf("%d ",arr[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{ int arr[20],n,i,j,temp;
clrscr();
printf("\n\n program to sort array using selection sort");
printf("\n enter no. of elements : ");
scanf("%d",&n);
printf("\n enter array now : \n");
for(i=0;i<n;i++)
{ printf(" enter a[%d] = ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{ for(j=i+1;j<n;j++)
{ if(arr[i]>arr[j])
{ temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("sorted list in ascending order = \n");
for(i=0;i<n;i++)
{ printf("%d ",arr[i]);
}
getch();
}

No comments:
Post a Comment
Guys if you think something is wrong or should be edit than please do comment.