I have an array of characters, which may look like:
1 0 0 1 1 1 0 0 0 1 1 0 1
I need to develop a search algorithm to find the best-fit hole in the array. I CANNOT use a linear search. (The problem is…I can’t think of any other way to do it).
Basically, if the input into my program is 3 bytes long, I need an function which would find the three 0’s in the array and use that spot to insert the data.
If the array is given to you as is (you didn’t create it), since you know the size of the hole you need, you can jump by size and look forward and back when you hit a
0to see if there’s room.If you’re trying to find a hole for 3 bytes and
a[0] == 1then you know it won’t fit ina[0] -> a[2]. Skip ahead toa[3]and see if that’s a0; if it is, look forward and back to see if there’s room for 3 bytes.This is only slightly better than the brute-force approach of going through the list start – finish, but it is better.