views:

123

answers:

3

Hey all, I'm trying to write a sort function but am having trouble figuring out how to initialize a value, and making this function work as a generic template. The sort works by:

Find a pair =(ii,jj)= with a minimum value = ii+jj = such at A[ii]>A[jj] If such a pair exists, then swap A[ii] and A[jj] else break;

The function I have written is as follows:

template <typename T>
void sort(T *A, int size)
{
 T min =453;
 T temp=0;
 bool swapper = false;
  int index1 = 0, index2 = 0;
  for (int ii = 0; ii < size-1; ii++){
   for (int jj = ii + 1; jj < size; jj++){
    if((min >= (A[ii]+A[jj])) && (A[ii] > A[jj])){
     min = (A[ii]+A[jj]);
     index1 = ii;
     index2 = jj;    
     swapper = true;
    }
   }
  }
  if (!swapper)
   return;
  else
  {
   temp = A[index1];
   A[index1] = A[index2];
   A[index2] = temp;
   sort(A,size);
  }
 }

This function will successfully sort an array of integers, but not an array of chars. I do not know how to properly initialize the min value for the start of the comparison. I tried initializing the value by simply adding the first two elements of the array together (min = A[0] + A[1]), but it looks to me like for this algorithm it will fail. I know this is sort of a strange type of sort, but it is practice for a test, so thanks for any input.

+2  A: 

most likely reason it fails, is because char = 453 does not produce 453 but rather different number, depending what char is (signed versus unsigned). your immediate solution would be to use numerical_limits, http://www.cplusplus.com/reference/std/limits/numeric_limits/

you may also need to think about design, because char has small range, you are likely to overflow often when adding two chars.

aaa
I know that I can't have char = 453, that is my problem. I don't know what to set this initial min value to, but I know it must be larger than the largest sum possible in the array.
wdow88
@wdow check the link, then u can use `numeric_limits<T>::max()` the greatest value of type, which you can use as your smallest value
aaa
+1 for pointing out the issue of Overflow.
Matthieu M.
+1  A: 

The maximum value of any type is std::numeric_limits<T>::max(). It's defined in <limits>.

Also, consider a redesign. This is not a good algorithm. And I would make sure I knew what I was doing before calling my sort function recursively.

rlbond
I understand that this is a very poorly designed algorithm. I did not come up with the algorithm, it is just an example that I was told to implement. I suppose it would be better to use a while loop instead of calling sort recursively, or do you have a more elegant idea?
wdow88
A: 

I haven't put too much time reading your algorithm, but as an alternative to std::numeric_limits, you can use the initial element in your array as the initial minimum value. Then you don't have to worry about what happens if you call the function with a class that doesn't specialize std::numeric_limits, and thus can't report a maximum value.

Dennis Zickefoose