I have a built-in array of integers and I would like to find the first occurrence of a given value in that array, without having to iterate through it.
Is there a .Net/Mono collection command that will provide this? And if so, what’s the syntax for that?
I’ve never heard of any Structure that would allow you to pull the first occurrence of a user specified value without any movement through the list/array, Unless that value was always the same and you organized the structure to always be the first value pop up.
Other then that as you populated array and kept that value that you were looking for and marked that location in a variable.
I am however limited by my current knowledge. I’m sure there are much more knowledgeable programmers that may offer further advice.
*Any way you could possibly post what your task your trying to complete?
Binary Search still iterates through your list. It probably even sorts it before hand as well. Since BST(Binary Search Tree) is a structure like a Reversed Tree it organizes your data in a way that it will travel to either the left or the right of the root value…
Yeah no problem. I just misunderstood what you meant. Yeah BST’s are Logarithmic O( n log(n) ) speed while Simple sorting ( when you use two loops and such) are most often O(n^2)… Much more efficient.
Anyways good to know how to use the sort without writing your own like I always have had to do in college. Nice to see other Oregonian’s on here.
You can call Array.Sort separately - the MSDN doc says that uses Quicksort - O(n log n) on average but O (n^2) worst case.
You’re right, it’s good to know the underlying algorithms. I had a phone interview with Google years ago, where they asked this exact question - how would you find a specific integer value in a sorted array? They were obviously asking for me to describe binary search (although my real life answer would be I probably wouldn’t bother and would call the most convenient built-in function).