Solved

shell sort

Posted on 2007-03-26
3
223 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:btocakci
  • 2
3 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18792260
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
0
 

Author Comment

by:btocakci
ID: 18792335
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.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 18792473
That should do it:

int makeEven(int i)
{
    return ((i%2) == 0)?  i : --i;  
}

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

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

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
copyEvens challenge 6 98
word0 challenge 3 91
wordmultiple challenge 12 135
Not seen Link button 5 39
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article will show, step by step, how to integrate R code into a R Sweave document
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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