Craig
Craig

Reputation: 183

Keep track of swaps and comparisons

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 .

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

Here is my code:

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

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

    swap(list, loc, minIndex);

   }
cout<<"swaps = "<<swaps<<endl;
  } //end selectionSort

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

 template <class elemType>
 int minLocation(elemType list[], int first, int last)
 {
  int loc, minIndex;    
  minIndex = first;

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

}

   return minIndex;
  } //end minLocation

  template <class elemType>
  void insertionSort(elemType list[], int length)
   {
int swaps = 0;
int comp = 0;

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

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

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

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

 #include<iostream>
 #include <ctime>
 #include <cstdlib>
 #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<1000; i++)
{
    a[i] = rand()% 1000;
    b[i] = a[i];
}


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

    return 0;
 }

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.

Upvotes: 0

Views: 1632

Answers (1)

Eric Z
Eric Z

Reputation: 14515

Actually, your selection sort has a fixed number of comparations (length * (length-1) / 2) and swaps (length).

If you want to count them anyway,

 // Count variables can be defined in main(), and passed through
 // to swap(), minLocation().
 template <class elemType>
 void swap(elemType list[], int first, int second)
 {
    // Count swap here  
 }

 template <class elemType>
 int minLocation(elemType list[], int first, int last)
 {
   ..
   for (loc = first + 1; loc <= last; loc++)
   { 
      // Count comparation here
   }   
   ..
 }

By the way, your counting of comparations in insertion sort is not complete either.

for (int firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder++)
{    
   // You miss the count here.
   if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])

Upvotes: 2

Related Questions