Sort {5, 1,12,-5, 16,2,12,14} using selection short
#incclude <algorithm>
int main() {
int arr[] = {5, 1,12,-5, 16,2,12,14};
int size = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < size; i++) {
int iMin = i;
for (int j = i + 1; j < size; j++) {
if (arr[iMin] > arr[j]) {
iMin = j;
}
}
std::swap(arr[i], arr[iMin]);
}
return 0;
}
Comments
Leave a comment