Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Created April 1, 2019 10:42
Show Gist options
  • Save jatinsharrma/de92f6465734f99e716d0b9313306938 to your computer and use it in GitHub Desktop.
Save jatinsharrma/de92f6465734f99e716d0b9313306938 to your computer and use it in GitHub Desktop.
Sorting an array - Selection Sort
#include <stdio.h>
int SelectionSort(int array[], int size){
for (int i =0 ; i < size ; i++){
int small = array[i], index = i;
for (int j = i ; j<size ; j++){
if (array[j] < small){
small = array[j];
index = j;
}
}
if (small != array[i]){
array[index] = array[i];
array[i] = small;
}
}
for (int i = 0 ; i<size ; i++){
printf("%d\n",array[i]);
}
}
void main() {
int array[] = {9,8,7,6,5,4,3,2,1,0};
int size = sizeof(array)/sizeof(array[0]);
SelectionSort(array,size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment