Skip to content

Instantly share code, notes, and snippets.

@YassineBajdou
Created October 23, 2018 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YassineBajdou/1a2f7ce5a117263f44eed0694e3d3d08 to your computer and use it in GitHub Desktop.
Save YassineBajdou/1a2f7ce5a117263f44eed0694e3d3d08 to your computer and use it in GitHub Desktop.
void selection_sort (int A[ ], int n) {
// temporary variable to store the position of minimum element
int minimum;
// reduces the effective size of the array by one in each iteration.
for(int i = 0; i < n-1 ; i++) {
// assuming the first element to be the minimum of the unsorted array .
minimum = i ;
// gives the effective size of the unsorted array .
for(int j = i+1; j < n ; j++ ) {
if(A[ j ] < A[ minimum ]) { //finds the minimum element
minimum = j ;
}
}
// putting minimum element on its proper position.
swap ( A[ minimum ], A[ i ]) ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment