如何在c ++中使用shellsort对三维数组[5] [5] [5]进行排序?

时间:2015-06-03 02:49:51

标签: c++ arrays shellsort

大家好我的程序有问题,我有一个tridimensiona数组,但我可以用shellsort排序。我可以显示数组,但void ordenacionShell无法排序我的数组,有人可以帮我吗?

llvm-src/include/llvm/Bitcode/LLVMBitCodes.h

正确工作的简单数组的原始Shellsort函数是:

#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;
}

1 个答案:

答案 0 :(得分:0)

您的阵列初始化错误。你到处都有流浪逗号。

示例:

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 ^

修复可以解决您的问题吗?