/***********************************************/ // R. A. Hillyard // //Selection sort for arrays /***********************************************/ #include<iostream> using namespace std; void sort(int a[], int size); void printArray(int a[], int size); const int MaxListSize = 100; int main() { int list[MaxListSize]; int temp = 0; int index = 0; cout << "enter integers to sort (-1 to end)\n> "; cin >> temp; while(temp != -1) { list[index++] = temp; cout << "> "; cin >> temp; }//end while sort(list, index); printArray(list, index); return 0; }//end main /***********************************************/ void sort(int list[], int listSize) { int temp = 0; int j; int i; int smallest = 0; int smallestIndex = 0; for(i = 0; i < listSize; i++) { smallest = list[i]; smallestIndex = i; for(j = i+1; j < listSize; j++) { if(list[j] < smallest) { smallest = list[j]; smallestIndex = j; } }//end for j temp = list[i]; list[i] = smallest; list[smallestIndex] = temp; }//end for i }//end sort /***********************************************/ void printArray(int a[], int size) { int j=0; cout << "sorted array \n"; while(j < size) cout << a[j++] << endl; } /****************************Program Output***************************/ enter integers to sort (-1 to end) > 7 > 3 > 56 > 7 > 9 > 2 > 4 > 7 > 12 > 4 > 94 > -1 sorted array 2 3 4 4 7 7 7 9 12 56 94