Link to home
Start Free TrialLog in
Avatar of btocakci
btocakciFlag for Türkiye

asked on

shell sort

i want to sort only the elements with even indexes in array using shellsort. since i didnot see the algorithm clearly , i cant edit it. my code for shellsort is:

template <class type>

void shellSort(type A[], int size)
{
  int i, j, incrmnt;
  type temp;

  incrmnt = size / 2;
  while (incrmnt > 0)
  {
        for (i=incrmnt; i < size; i++)
    {
      j = i;
      temp = A[i];
      while ((j >= incrmnt) && (A[j-incrmnt] > temp))
      {
        A[j] = A[j - incrmnt];
        j = j - incrmnt;
      }
      A[j] = temp;
    }
    incrmnt /= 2;
  }
}

where i should edit, any help?
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

What happens with the odd indexes? If it doesn't matter or if they sould be sorted with their even neighbor, you could do that:

template <class type>
void shellSort(type B[], int size)
{

  struct Even
  {
     type x[2];
     bool operator> (const Even& e) const
     { return x[0] > e.x[0]; }
  };

  Even* A = reinterpret_cast<Even*>(B);
  size /= 2;
 
   // below use your code
   ...
}

Regards, Alex
Avatar of btocakci

ASKER

odd indexes will remain same.eg:

12 8 2 15 68 94 49 -> 2   8  12  15  49  94  68
i will try to sort this new array with another algortihm. just to understand the algorithms more.
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial