Solved

Count comparisons in sort algorithms??

Posted on 2003-10-27
7
701 Views
Last Modified: 2007-12-19
I'm interested in tracking the number of comparisons in some sort algorithms to compare them.
Im not exactly sure where this counter need to be incremented.  If anyone has any help, thanks.
Some of the sorts I am using include the following.

INSERTION
-------------------------------------------------------------------------------------
template <class T>
void InsertionSort(T A[], int size)
{
  for(int j=1; j<size; j++)
    {
      T key = A[j];
      int i = j - 1;
      while ((i >= 0) && (A[i] > key))
        {
          A[i+1] = A[i];
          i -= 1;
        }
      A[i+1] = key;
    }
}

HEAPSORT
------------------------------------------------------------------------------
template <class T>
void Heapsort(T A[], int size)
{
  BuildHeap(A, size);
  for(i=size; i>1; i--)
    {
      int tmp = A[0];
      A[0] = A[i-1];
      A[i-1] = tmp;
      size--;
      Heapify(A, 1, size);
    }
}

template <class T>
void BuildHeap(T A[], int size)
{
  for(i=size/2; i>0; i--)
    Heapify(A, i, size);
}

template <class T>
void Heapify(T A[], int i, int size)
{
  int  l = 2 * i;
  int  r = 2 * i + 1;
  int largest = 0;

  if(l <= heapsize && A[l-1] > A[i-1])
    largest = l;
  else
    largest = i;

  if(r <= heapsize && A[r-1] > A[largest-1])
    largest = r;
  if(largest != i)
    {
      int tmp = A[i-1];
      A[i-1] = A[largest-1];
      A[largest-1] = tmp;
      Heapify(A, largest, heapsize);
    }
}

0
Comment
Question by:killer455
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:MrNed
ID: 9631526
Um, you would need to increment a counter everytime you compare elements in the array.

INSERTION - Inside the while loop
HEAPSORT - Twice for each call of Heapify()

I dont think you really want comparisons of non-array elements.
0
 

Author Comment

by:killer455
ID: 9631890
So if I were to place a counter in the heapify function, is there a way I can use that in the original heapsort function in order to produce output after heapsort is done running?
Does this run into making the variable global? If so how is this done?
0
 
LVL 7

Expert Comment

by:MrNed
ID: 9631900
There are many ways you could do it:
1. Use a global variable to count the number of comparisons
2. Create a property for the class and use that to store the count
3. Return the count from the Heapify function

Decide what you're after and have a go. Im not inclined to do too much until I can see you're trying to figure it out.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:killer455
ID: 9635830
I guess im confused, sorry :(  I modofied it to return the compares??

HEAPSORT
------------------------------------------------------------------------------
template <class T>
void Heapsort(T A[], int size)
{
  BuildHeap(A, size);
  for(i=size; i>1; i--)
    {
      int tmp = A[0];
      A[0] = A[i-1];
      A[i-1] = tmp;
      size--;
      Heapify(A, 1, size);
    }
}

template <class T>
void BuildHeap(T A[], int size)
{
  for(i=size/2; i>0; i--)
    Heapify(A, i, size);
}

template <class T>
void Heapify(T A[], int i, int size)
{
  int compares;
  comapres +=2;
  int  l = 2 * i;
  int  r = 2 * i + 1;
  int largest = 0;

  if(l <= heapsize && A[l-1] > A[i-1])
    largest = l;
  else
    largest = i;

  if(r <= heapsize && A[r-1] > A[largest-1])
    largest = r;
  if(largest != i)
    {
      int tmp = A[i-1];
      A[i-1] = A[largest-1];
      A[largest-1] = tmp;
      Heapify(A, largest, heapsize);
    }
return compares;
}
0
 
LVL 7

Accepted Solution

by:
MrNed earned 60 total points
ID: 9637411
You declared the int compares locally inside the Heapify function. This means that when the function exits, that variable is wiped out. To return it successfully, you need to modify the function declaration to return an int rather than a void.

Since you call the Heapify function recursively, you should capture the return value of the Heapify (A, largest, heapsize) call and add that to you compares counter before returning it.
0
 

Author Comment

by:killer455
ID: 9637633
Well I cant declare it in the heapsort() function because heapify is called both from buildheap and heapsort right?

If I capture it only from Heapify(A, largest, heapsize) won't that make the count on compares inaccurate?

Well sorry for the additional questions, i'm just very confused on how this is done.  If I didnt mention im a beginning programmer so some things that are basically obvious to advanced programmers, are kinda new to me.

Thanks for your continued help.

Ben

0
 
LVL 7

Expert Comment

by:MrNed
ID: 9647698
This is what im thinking, although there are other ways to do it.

template <class T>
void Heapsort(T A[], int size)
{
int count;
  count = BuildHeap(A, size);
  for(i=size; i>1; i--)
    {
      int tmp = A[0];
      A[0] = A[i-1];
      A[i-1] = tmp;
      size--;
      count += Heapify(A, 1, size);
    }
printf("count = %d\n",count);
}

template <class T>
int BuildHeap(T A[], int size)
{
int count=0;
  for(i=size/2; i>0; i--)
    count += Heapify(A, i, size);
return count;
}

template <class T>
int Heapify(T A[], int i, int size)
{
  int count;
  count +=2;
  int  l = 2 * i;
  int  r = 2 * i + 1;
  int largest = 0;

  if(l <= heapsize && A[l-1] > A[i-1])
    largest = l;
  else
    largest = i;

  if(r <= heapsize && A[r-1] > A[largest-1])
    largest = r;
  if(largest != i)
    {
      int tmp = A[i-1];
      A[i-1] = A[largest-1];
      A[largest-1] = tmp;
      count += Heapify(A, largest, heapsize);
    }
return count;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unresolved External Symbols 3 68
Unable to start eclipse ? 17 132
c++, dynamic object by json 1 24
Error creating a new C++ project in ,net 20 21
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now