2

これを機能させることができません。何をしても正しくソートされないようです。

ポイント数に基づいて降順に並べ替えようとしています。

Bryan_Bickell         2    5    +2

Brandon_Bolig         0    3     0

Dave_Bolland          4    2    -1

Sheldon_Brookbank     0    4   -1

Daniel_Carcillo       0    1    +3

真ん中の列はポイント数です。

これらの値をすべて格納するために 4 つの配列を使用していますが、配列選択ソートを正しく利用して正しい方法で並べ替えるにはどうすればよいでしょうか?

以下のすべての答えを試しましたが、どれもうまくいかないようでした。これが私がこれまでに持っているものです

void sortArrays( string playerNames[], int goals[], int assists[], int rating[], int numPlayers )
{
int temp, imin;
int points[numPlayers];

 for(int j = 0; j < numPlayers; j++)
    {
        points[j] = goals[j] + assists[j];
    }

    imin = points[0];

for(int i = 0; i < numPlayers; i++)
{
  if (points[i] < imin)
  {
        imin = points[i];
   }
}

 for(int j = 1; j < numPlayers; j++)
{
    if (points[j] > imin)
    {
        temp = points[j];
          points[j] = points[j-1];
               points[j-1] = temp;
    }
}
}
4

3 に答える 3

3

このようになるはずです...

void selsort(int *a,int size)
{
   int i,j,imin,temp;
   //cnt++;
   for(j=0;j<size;j++)
   {
       //cnt+=2;
       imin=j;
       for(i=j+1;i<size;i++)
       {
           //cnt+=2;
          if(a[i]<a[imin])
          {
             //cnt++;
             imin=i;
          }
        }

        if(imin!=j)
        {
            //cnt+=3;
            temp=a[j];
            a[j]=a[imin];
            a[imin]=temp;
         }
    }
}
于 2013-03-29T19:39:22.400 に答える
1

中央の列のみがソートに使用される場合、つまり、レコードのソートに使用されるキーが使用される場合、これらのレコードを格納するために 4 つの配列は必要ありません。私の理解では、選択ソートを使用してポイント数に基づいて人々のレコードをソートしようとしています。コードは次のようになりますrecords

void selectionSort(RECORD records[], int n) {
  int i, j, minIndex, tmp;    
  for (i = 0; i < n - 1; i++) {
        maxIndex = i;
        for (j = i + 1; j < n; j++)  //find the current max
        {
              if (records[j].point > records[minIndex].point)
              {
                    //assume point is the number of point, middle column
                    minIndex = j;
              }
        }

        //put current max point record at correct position
        if (minIndex != i) {
              tmp = records[i];
              records[i] = records[minIndex];
              records[minIndex] = tmp;
        }
  }
}

必要に応じて、すべてのレコードを「降順」で並べ替えます

于 2013-03-29T19:41:37.663 に答える
0

データを std::vector に保存してからソートするのはどうですか

int compare(int a, int b){
 return (a>b);
}

void sort(std::vector<int> &data){
 std::sort(data.begin(), data.end(), compare);
}

可能な限りベクトルを使用するようにしてください。ベクトルは、パフォーマンスとメモリ使用量の向上のために大幅に最適化されています

于 2013-03-29T20:26:50.720 に答える