Skip to content

Instantly share code, notes, and snippets.

@thelinuxpoint
Last active September 22, 2022 05:44
Show Gist options
  • Save thelinuxpoint/e39a95138142abfd0a291874775fc016 to your computer and use it in GitHub Desktop.
Save thelinuxpoint/e39a95138142abfd0a291874775fc016 to your computer and use it in GitHub Desktop.
//
template <class T>
int indexOfMax(T a[], int size) {
int temp = 0;
int index = 0;
for ( int i = 0; i < size; i++ ) {
if ( a[i] > temp ) {
temp = a[i];
index = i;
}
}
return index;
}
//
template <class T>
void swap( T &x, T &y ) {
T temp = a;
a = b;
b = temp;
}
// the algorithm
template <class T>
void selectionSort(T a[], int n) {
for ( int size = n; size > 1; size-- ){
int j = indexOfMax(a,size);
swap(a[j],a[size - 1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment