I have a master list integer array which has around 500 numbers. And, i have a set of 100 randomized number which has picked from the master list to find the missing numbers. Now, I need to go through this randomized number list against the master list. What would be the best approach in C programming to go through it without hanging the program. If i go through in simple ‘for’ loop for 500 elements, it will hang as it needs to go through the entire list. Could someone direct me on this?
Thanks.
First, you should profile it. It’s only 500*100=50,000 operations at the max we’re talking about. An average modern computer is capable of finishing it off in under one-tenth of a second, unless you code it very inefficiently.
Assuming that you would like to optimize it anyway, you should sort the master array, and run a binary search on it for each element of the randomized array. This would reduce the number of operations from 50,000 to at most 900, because a binary search of 500 numbers requires at most 9 comparisons.
Here is an implementation that uses built-in sorting and binary search functions (
qsortandbsearch) of the standard C library:Here is a link to this running program on ideone.