所有数组元素的比较

编程入门 行业动态 更新时间:2024-10-26 03:27:57
本文介绍了所有数组元素的比较 - Ç算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个矩阵的 M 的*的 N 的和,对于每一行,我需要的所有元素当中比较。对于每对夫妇,我觉得,我会打电话给该会执行一些计算功能。

I have a matrix m * n and, for each row, I need to compare all elements among them. For each couple I find, I'll call a function that is going to perform some calculations.

例如:

my_array -> {1, 2, 3, 4, 5, ...} I take 1 and I have: (1,2)(1,3)(1,4)(1,5) I take 2 and I have: (2,1)(2,3)(2,4)(2,5) and so on

用C我写这样的:

Using C I wrote this:

for (i=0; i<array_length; i++) { for (k=0; k<array_length; k++) { if (i==k) continue; //Do something } } }

我想知道如果我可以使用算法复杂度低。

I was wondering if I can use an algorithm with lower complexity.

推荐答案

没有,这是为O(n ^ 2)的定义[长在这里解释,但请相信我( - :]结果但是你可以通过减少一半的迭代次数:

No, it's O(n^2) by definition [ to long to explain here, but trust me (-: ] But you can decrease the number of iterations by half :

for (i=0; i<array_length; i++) { for (k=i+1; k<array_length; k++) { // <-- no need to check the values before "i" //Do something //If the order of i and k make a different then here you should: //'Do something' for (i,k) and 'Do something' for (k,i) } } }

更多推荐

所有数组元素的比较

本文发布于:2023-11-30 11:07:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   元素

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!