Keep track of swaps and comparisons
<p>I am writing a sorting program that needs to take a an array, fill it with 1000 random numbers, then copy the array into a second array.  After that the program is supposed to use the selectionSort function and the insertSort function .</p>

<p>But when I use the functions I am supposed to keep track of all the swaps and key comparisons.  I have figured out how to do that for the insertSort.  I cannot figure out how to do this for the selectionSort</p>

<p>Here is my code:</p>

<pre><code> template &lt;class elemType&gt;
 void selectionSort(elemType list[], int length)
 {
    int loc, minIndex;
int swaps =0;

  for (loc = 0; loc &lt; length; loc++)
  {
    minIndex = minLocation(list, loc, length - 1);

    swap(list, loc, minIndex);

   }
cout&lt;&lt;"swaps = "&lt;&lt;swaps&lt;&lt;endl;
  } //end selectionSort

 template &lt;class elemType&gt;
 void swap(elemType list[], int first, int second)
 {
 elemType temp;
  temp = list[first];
  list[first] = list[second];
  list[second] = temp;
  } //end swap

 template &lt;class elemType&gt;
 int minLocation(elemType list[], int first, int last)
 {
  int loc, minIndex;    
  minIndex = first;

  for (loc = first + 1; loc &lt;= last; loc++)
   { if (list[loc] &lt; list[minIndex])
        minIndex = loc;

}

   return minIndex;
  } //end minLocation

  template &lt;class elemType&gt;
  void insertionSort(elemType list[], int length)
   {
int swaps = 0;
int comp = 0;

   for (int firstOutOfOrder = 1; firstOutOfOrder &lt; length;
                             firstOutOfOrder++)
    if (list[firstOutOfOrder] &lt; list[firstOutOfOrder - 1])
    {
        elemType temp = list[firstOutOfOrder];
        int location = firstOutOfOrder;

        do
        {
            list[location] = list[location - 1];
            location--;
            comp +=1;   
        } while(location &gt; 0 &amp;&amp; list[location - 1] &gt; temp);

        list[location] = temp;
         swaps +=1;
    }

cout&lt;&lt;"swaps = "&lt;&lt;swaps&lt;&lt;endl;
cout&lt;&lt;"comps = "&lt;&lt;comp&lt;&lt;endl;
   } //end insertionSort

 #include&lt;iostream&gt;
 #include &lt;ctime&gt;
 #include &lt;cstdlib&gt;
 #include "searchSortAlgorithms.h"

 using namespace std;

 int main (){
//generate a new random set each time 

srand(time(0));

int a[1000] = {0};
int b[1000] = {0};

for(int i= 0; i&lt;1000; i++)
{
    a[i] = rand()% 1000;
    b[i] = a[i];
}


insertionSort(a, 1000);
selectionSort(b, 1000);

    return 0;
 }
</code></pre>

<p>The swaps and comparisons print out for the inertionSort, but I am unfamiliar with how I would get it to work with the selectionSort since the sort algorithm calls other functions in a for loop.
Any input would be appreciated it.</p>
