Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 查找数字数组中两个最近元素之间的距离_Arrays - Fatal编程技术网

Arrays 查找数字数组中两个最近元素之间的距离

Arrays 查找数字数组中两个最近元素之间的距离,arrays,Arrays,com论坛 我试图在C++中编译下面的算法。有什么想法吗 // Find the distance between the two closest elements in an array of numbers. // Minimum Distance // dmin = infinity; // for i = (i to n - 2) do // for j = i + 1 to n - 1 do // temp = A[i] - A[j] // if temp < dmin; //

com论坛

我试图在C++中编译下面的算法。有什么想法吗

// Find the distance between the two closest elements in an array of numbers.
// Minimum Distance
// dmin = infinity;
// for i = (i to n - 2) do
// for j = i + 1 to n - 1 do
// temp = A[i] - A[j]
// if temp < dmin;
// dmin = temp;
// return dmin;
//查找数字数组中两个最近元素之间的距离。
//最小距离
//dmin=无穷大;
//对于i=(i到n-2)do
//对于j=i+1到n-1 do
//温度=A[i]-A[j]
//如果温度
我建议您不要使用该算法(
O(n^2)
),而是对数字进行排序,然后进行线性扫描(
O(nlogn)
)。

是的,phant0m是正确的,但排序算法本身将使用循环

排序算法通常根据其效率来判断。在这种情况下,效率是指当输入的大小变大时的算法效率,通常基于要排序的元素数量。使用中的大多数算法的算法效率为O(n^2)或O(n*log(n))。(http://www.cprogramming.com/tutorial/computersciencetheory/sortcomp.html)


除非数组已经排序了,或者以后需要排序的版本,或者数组可能变得非常大,否则您最好“咬紧牙关”,坚持原来的方法

这不是一个解决家庭作业的网站。你是否有一个你正在努力解决的实际问题,而不是“我什么都不知道”?