Skip to content

Instantly share code, notes, and snippets.

@LucasPLopes
Created August 19, 2019 12:30
Show Gist options
  • Save LucasPLopes/df23bd3bb5962a0e6a5f520acdcb8b6c to your computer and use it in GitHub Desktop.
Save LucasPLopes/df23bd3bb5962a0e6a5f520acdcb8b6c to your computer and use it in GitHub Desktop.
Selection sort in C
#include<stdio.h>
void selectionsort(int *v, int n){
int index;
int aux ;
for(int i = 0; i < n; i++)
{
index = i;
for(int j = i +1 ; j< n - 1; j++){
if(v[j] < v[index])
index = j;
}
if(index != i)
{
aux = v[index];
v[index] = v[i];
v[i] = aux;
}
}
}
int main(){
int v[10] = {5,2,1,4,3,6,8,9,7,10},
length = sizeof(v)/sizeof(int);
selectionsort(v,length);
for (size_t i = 0; i < length; i++)
{
printf("%i\t", v[i]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment