Last active
September 22, 2022 05:44
-
-
Save thelinuxpoint/e39a95138142abfd0a291874775fc016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
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