// Example 1-11 Selection sort #include using namespace std; void print(int a[], int size); void selection_sort(int a[], int size); void swap(int& a, int& b); int main() { int array[] = { 7,9,6,2,5,3 }; int size = sizeof(array) / sizeof(int); selection_sort(array,size); print(array,size); return 0; } void print (int a[], int size) { int i; for (i = 0; i < size; i++) { cout << a[i] << '\t'; } cout << endl; } void selection_sort(int a[], int size) { // minIndex is the position in the unsorted part containing the minimum int minIndex; for (int i = 0; i < size - 1; i++) { // find the position of the minimum in the unsorted part minIndex = i; for (int j = i+1; j < size; j++) { if (a[j] < a[minIndex]) { minIndex = j; } } // swap the value of the minimum in the unsorted part with the next // value in the sorted part if(minIndex != i) // don’t swap if the minimum is already in place { swap(a[i],a[minIndex]); } } } void swap(int& a, int& b) { int temp; temp = a; a = b; b = temp; }