Solved

shell sort

Posted on 2007-03-26
3
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
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…

726 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