btocakci
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?
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?
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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