【问题标题】:How to use selection sort algorithm correctly to sort a list?如何正确使用选择排序算法对列表进行排序?
【发布时间】:2013-03-30 04:52:42
【问题描述】:

我无法让它工作,似乎无论我做什么它都无法正确排序。

我正在尝试根据点数按降序排序。

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

【问题讨论】:

  • 我猜你需要修改你的想法......选择排序处理在每次迭代中找到最小值......你没有设置任何最小值......请参考stackoverflow.com/questions/8362640/…
  • 你的“交换”有问题;请注意,变量temp 从未设置过。此外,您的代码看起来像是冒泡排序的一部分,而不是选择排序。
  • 这里有std::swap ;) -> using std::swap; ... swap(points[i], points[i+1]);
  • 如果您有 4 个并行数组,则您的交换代码需要并行处理所有 4 个数组。使用一组结构会做得更好;大概,你还没有涵盖。
  • 我想先对点进行排序,然后在函数关闭后处理所有 4 个数组将是一件轻而易举的事。

标签: c++ arrays algorithm sorting selection


【解决方案1】:

应该是这样的……

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

【讨论】:

  • a 将是您所需的数组
  • 我不应该是 j
  • 调用是这样的...for(i=0;i&lt;ip;i++) a[i]=i; selsort(a,ip);
【解决方案2】:

如果只使用中间列进行排序,则不需要 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;
        }
  }
}

它会按你想要的“降序”对你的所有记录进行排序

【讨论】:

  • 注意。如果使用int records[],.point 位不正确...我认为这是来自另一个使用结构的草稿?
  • @Useless 是的,我很粗心,我的意思是一个结构数组。谢谢!
【解决方案3】:

如何将数据存储到 std::vector 中然后对其进行排序

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

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

尽量使用向量,它们已针对性能和更好的内存使用进行了大量优化

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2021-12-04
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多