gpt4 book ai didi

algorithm - 选择排序顺序

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:16 25 4
gpt4 key购买 nike

我的书说:

Suppose you have a group of N numbers and would like to determine the kth largest. This is known as the selection problem. Most students who have had a programming course or two would have no difficulty writing a program to solve this problem. There are quite a few “obvious” solutions. One way to solve this problem would be to read the N numbers into an array, sort the array in decreasing order.

它表示按降序对数组进行排序是有意义的。这有什么意义?如果我有一个 {1,9,3,7,4,6​​} 数组并且我想要最大的元素,我会按升序对它进行排序,这样 {1,3, 4,6,7,9} 然后返回最后一个元素。为什么书上会说降序?

最佳答案

因为你可能不想要最大的元素,书上说

would like to determine the kth largest

如果你按升序排序,你怎么知道第三大的数是多少而不首先找出数组有多大?

如果数组是递减的,这会更容易,因为第三大元素就是第三个元素。

关于algorithm - 选择排序顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53174527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com