【问题标题】:Algorithm to determine the order of items based on weighted values基于加权值确定项目顺序的算法
【发布时间】:2015-05-28 15:23:43
【问题描述】:

我什至不知道从哪里开始处理这类问题,希望有人能指出我正确的方向。

我有一个项目列表,每个项目都有四个属性。对属性进行权衡,例如属性1比属性2更重要,等等。

我希望找到某种算法或方程式来根据这些值对这些项目进行排序。想法?

【问题讨论】:

  • standard sorting algorithms 有什么问题?
  • 哈哈......如果这是最好的方法,那就没什么了。就像我提到的......我什至不知道从哪里开始。我会检查一下。谢谢!
  • 实现您自己的比较器并使用标准排序算法..

标签: algorithm sorting


【解决方案1】:

你应该使用regular sorting algorithm,唯一的区别是你的比较器,说x<y更复杂,应该类似于下面的伪代码:

compare(x,y) {
   if (x.attribute1 < y.attribute1) return -1
   if (x.attribute1 > y.attribute1) return 1
   if (x.attribute2 < y.attribute2) return -1
   if (x.attribute2 > y.attribute2) return 1
   if (x.attribute3 < y.attribute3) return -1
   if (x.attribute3 > y.attribute3) return 1
   if (x.attribute4 < y.attribute4) return -1
   if (x.attribute4 > y.attribute4) return 1
   return 0
}

另一种方法是使用stable sorting algorithm,对每个属性重复排序,从最不重要的开始,到最重要的结束。

【讨论】:

    【解决方案2】:

    如果您想(按字典顺序)对您的东西进行排序,例如:

    1234 1244 第1312章

    您可以使用Radix algorith

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多