Skip to content

Instantly share code, notes, and snippets.

@rohit-nsit08
Created September 2, 2011 16:50
Show Gist options
  • Save rohit-nsit08/1189134 to your computer and use it in GitHub Desktop.
Save rohit-nsit08/1189134 to your computer and use it in GitHub Desktop.
simple selection sort
//selection sort
#include<stdio.h>
int main()
{
int arr[5]={5,4,3,2,1};
int i,j,smallest,t;
int n = sizeof(arr)/sizeof(int);
for(i=0;i<n-1;i++)
{
smallest= i;
for(j=i;j<n;j++) // search for the next smallest element
{
if(arr[j]<arr[smallest])
smallest = j;
} // place the next smallest element in its correct
t = arr[i]; // position
arr[i] = arr[smallest];
arr[smallest]=t;
}
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment