Monday, 27 October 2014

write a C program for Selection sort

#include <stdio.h> #include <conio.h> #define MAXSIZE 50 int arr[MAXSIZE],n; void selectionsort(int arr[],int n); void main() { int i; printf("Enter the size of array\n"); scanf("%d",&n); printf("\nEnter the values \n"); for(i=0;i<n;i++) scanf("%d",&arr[i]); printf("\nArray before sorting \n"); for(i=0;i<n;i++) printf("\t%d",arr[i]); printf("\n\n"); selectionsort(arr,n); printf("\nArray after sorting \n"); for(i=0;i<n;i++) printf("\t%d",arr[i]); } void selectionsort(int arr[],int n) { int i,j,temp,min; for(i=0;i<n-1;i++) { min=i; for(j=i+1;j<n;j++) { if(arr[min]>arr[j]) { min=j; } temp=arr[i]; arr[i]=arr[min]; arr[min]=temp; } } }

No comments:

Post a Comment