Solved

shell sort

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 106
firstChar challenge 13 121
rhino JavaScript import, load 25 92
designing in object programming 12 94
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

840 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