0

Hi everyone i have a problem with my program, i have a tridimensiona array but i can sort with shellsort.I can show the array, but the void ordenacionShell cant sort my array,can anyone help me please?

#include <iostream>

using namespace std;
void intercambiar(int& x, int& y);

void intercambiar(int& x, int& y)
{
int aux = x;
x = y;
y = aux;
}
void ordenacionShell(int a[5][5][5], int n)

 {
int salto, i, j, k,j1,j2,k1,k2;
salto = n / 2;
while (salto > 0)
{
for (i = salto; i < n; i++)
{
    j = i - salto;
    j1= i - salto;
    j2= i - salto;
    while (j >= 0 )
    {
        k = j + salto;
            k1 = j + salto;
                k2 = j + salto;
        if (a[j][j1][j2] <= a[k][k1][k2])
        {j = -1; // par de elementos ordenado
            j1 = -1;
            j2 = -1;}
        else
        {
            cout<<"intercambio: "<<"";
            cout<<a[j][j1][j2]<<" ";
            cout<<a[k][k1][k2]<<"\n";
            intercambiar(a[j][j1][j2], a[k][k1][k2]);

            j -= salto;
            j1 -= salto;
            j2 -= salto;


        }
    }
  }
   salto = salto / 2;
   cout<<"Salto: "<<salto<<"\n";
  }
}


int main()
{
int a[5][5][5] = {
    { {1,2,9,4,5}, {6,7,17,9,10}, {11,12,16,14,15}, {16, 17, 22, 19, 20},   {21,22, 20, 24, 25} },

   {26,25,28,29,30}, {31,29,33,34,35 }, {36,30,38,39,40}, {41,42,49,44,45},      {46, 47, 34, 49, 50},  },

   { {51, 52, 49, 54, 55}, {56,57,58,50,60}, {61,62,63,66,65}, {66, 67, 68, 69, 71}, {71, 70, 73, 74, 75}, },

 { {76, 75, 78, 79, 80}, {81,82,73,84,85}, {86,77,88,89,90}, {91, 82, 93, 94, 95}, {96, 91, 98, 99, 100}, },

  { {101, 100, 103, 104, 105}, {106,105,108,109,110}, {121, 112, 113, 114, 115}, {116, 117, 118, 119, 121}, {121, 122, 123, 124, 123} }
};

for(int i=0;i<5;i++)
{
   for(int j=0;j<5;j++)
   {
         for(int l=0;l<5;l++)
   {
    cout<<a[i][j][l]<<",";

   }
  }}
   ordenacionShell( a,5);
        for(int i=0;i<5;i++)
     {
      for(int j=0;j<5;j++)
        {
         for(int l=0;l<5;l++)
       {
       cout<<a[i][j][l]<<",";

    }
 }}


  return 0;
}

The original Shellsort function for a simple array correctly working is this:

void ordenacionShell(int a[], int n)
{
int salto, i, j, k;
salto = n / 2;
while (salto > 0)
{
for (i = salto; i < n; i++)
{
    j = i - salto;
    while (j >= 0)
    {
        k = j + salto;
        if (a[j] <= a[k])
            j = -1; // par de elementos ordenado
        else
        {
            cout<<"intercambio: "<<"";
            cout<<a[j]<<" ";
            cout<<a[k]<<"\n";
            intercambiar(a[j], a[k]);
            j -= salto;
        }
    }
}
salto = salto / 2;
cout<<"Salto: "<<salto<<"\n";
}
}
3
  • Possible duplicate of stackoverflow.com/questions/20130673/… Commented Jun 3, 2015 at 2:54
  • nitpick -- intercambiar() is the same as std::swap() =) Commented Jun 3, 2015 at 3:46
  • What exactly does it mean to sort a 3-dimensional array? Are you treating the whole array as if it were 1-dimensional? Commented Aug 20, 2015 at 23:10

1 Answer 1

0

Your array initialization is wrong. You have wandering commas everywhere.

Example:

int a[5][5][5] = {
    { {1,2,9,4,5}, {6,7,17,9,10}, {11,12,16,14,15}, {16, 17, 22, 19, 20},   {21,22, 20, 24, 25} },

   {26,25,28,29,30}, {31,29,33,34,35 }, {36,30,38,39,40}, {41,42,49,44,45},      {46, 47, 34, 49, 50},  },
//                                                                                    check this out ^

Does fixing that fix your issue?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.