1 #include <iostream>
2
3 using namespace std;
4
5 int maxIndex(const int list[], int size)
6 {
7 int currentMaxIndex = 0;
8
9 for (int index = 1; index < size; index++)
10 {
11 if (list[index] > list[currentMaxIndex])
12 currentMaxIndex = index;
13 }
14
15 return currentMaxIndex;
16 }
17
18 void selectionSortIterative(int list[], int size)
19 {
20 for (int range = size; range > 1; range--)
21 {
22 swap( list[range - 1], list[ maxIndex(list,range) ] );
23 }
24 }
25
26 void selectionSortRecursive(int list[], int size)
27 {
28 if (size == 1)
29 return;
30 else
31 { swap( list[size - 1], list[ maxIndex(list,size) ] );
32 selectionSortRecursive(list, size - 1);
33 }
34 }
35
36 void printList(const int list[], int size)
37 {
38 for (int i = 0; i < size; i++)
39 cout << '\t' << list[i] << endl;
40 }
41
42 int main()
43 {
44 const int myListSize = 10;
45 int myList[myListSize] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
46
47 cout << "Before sorting, list is..." << endl;
48 printList(myList, myListSize);
49
50 selectionSortRecursive(myList, myListSize);
51
52 cout << "After sorting, list is..." << endl;
53 printList(myList, myListSize);
54
55 return 0;
56 }