sorting - Algorithm to find the smallest non negative integer that is not in a list -
Looking at a list of integers, how can I find an integer in the list not
The list may be too large, and the integer can grow (i.e., big intersea, not just 32-bit ints).
If it makes a difference, then the list is probably "sorted", i.e. 99% of the time will be sorted, but I can not always rely on sorting.
Edit -
To illustrate, the list is given {0, 1, 3, 4, 7}, examples of acceptable solutions -2 , 2, 8 and 10012, but I would like to find the smallest, non-negative solution (i.e. 2) if there is an algorithm that can get it without having to sort the entire list.
iterate the list to get the highest value n , Then you know that n + 1 is not in the list.
Edit:
To find the lowest positive unused number woul, a method must start with zero and scan the list of numbers, start and enlarge if you have a number It can be sorted to make it more efficient, and to use the high potential of the list, you can move the numbers that are smaller than the current for an unused portion of the list.
This method uses the beginning of the list as a storage space for a small number, startIndex variable keeps track of the number where relevant numbers start: < / P>
public static int GetSmallest (int [] items) {int startIndex = 0; Int results = 0; Int i = 0; While (i and lt; item length) {if (item [i] == result) {Results ++; I = startIndex; } And {if (item [i] I did an execution test where I made a random number 100,000 from 9999, which creates the average number around 150. On this test run (with every 1000 test list), this method was the lowest number in the order of 29 ms respectively, and in the orderly lists by average in 0.32 ms
(I checked this How did the method leave the list, because it can swap some items in it, leaves a list with at least one list, and it takes small values into the list, I think that the real It must be solved and more for each search.)
Comments
Post a Comment