

Best method to search for an array?
source link: https://www.codesd.com/item/best-method-to-search-for-an-array.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Best method to search for an array?
I have an array (nodes[][]
) that contains values of effective distances that looks something like this:
__ __
|1 0.4 3 |
|0.4 1 0 |
|3 3.2 1 ... |
|0.8 4 5 |
|0 0 1 |
-- --
Where the first value, node[0][0]
is the distance from node 0 to node 0 which is 1.
So the distance from node 2 to node 1 is 3.2 (node[2][1]=3.2
)
I need, given a node column, to search through the rows to find the farthest distance, while not picking itself (node[1][1]
)
The method I was thinking to do something like this:
int n=0;
currentnode=0; //this is the column I am searching now
if(currentnode==n)
n++;
best=node[n][currentnode];
nextbest=node[n++][currentnode];
if(nextbest>best)
best=nextbest;
else
for(int x=n;x<max;x++) //max is the last column
{
if(currentnode==n)
continue;
nextbest=node[x][currentnode];
if(nextbest>best)
best=nextbest;
}
I can't think of a better method to do this. I could use functions to make it shorter but this is GENERALLY what I am thinking about using. After this I have to loops this to go to the next column that the best distance returns and do this routine again.
You can simplify it quite a bit. A lot of your checks and temporary variables are redundant. Here's a small function that performs your search. I've renamed most of the variables to be a little more precise what their roles are.
int maxDistance(int fromNode) {
int max = -1;
for (int toNode = 0; toNode < nodeCount; ++toNode)
{
if (fromNode != toNode && nodes[toNode][fromNode] > max) {
max = node[toNode][fromNode];
}
}
return max;
}
Recommend
-
11
Alongside the plain object, the array is a widely used data structure in JavaScript. And a widely used operation on arrays is accessing elements by index. In this post, I’m going to present the new array method array.at(index)
-
17
How to Use Array Reduce Method in JavaScriptLet’s say you have an array of numbers: const numbers = [2, 4, 6]; How can you sum these numbers? Thanks to the array.reduce() method...
-
7
JavaScript array value of the method advertisements [].valueOf() method retuns array itself.According to this docu...
-
7
What is the effective method for adding an element to the array in java when the array size is small (not more than 5) advertisements The gene...
-
6
Using the every Method in an array Jul 9 ・1 min read ...
-
4
Using the new array.at() method There is a new array method on the block. It's array.at() and the main difference vs the normal square brackets access syntax is the support for the negative indexes. Let's conside...
-
7
Hi Everyone 👋 Today I wanted to share some of the most common use cases for Array sort() method in JavaScript. The...
-
5
Method not found !!0[] System.Array.Empty()-黑暗執行緒 部署 ASP.NET MVC 程式遇到奇怪錯誤: Server Error in '/' Application. Method not found: '!!0[] System.Array.Empty()'. Description: An unhandled exce...
-
8
ECMAScript 2022 introduces at() method in Array, String, TypedArray Feb 24, 2022 , by Chetan Gawai 2 minute read
-
3
If you need to split up a string into an array of substrings, then you can use the JavaScript split() method. In this article, I will go over the JavaScript split() method and provide code examples.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK