Solved

shell sort

Posted on 2007-03-26
3
219 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

746 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

13 Experts available now in Live!

Get 1:1 Help Now