# Array searching and scoring?

Sam Bluesman
Ranch Hand
Posts: 191
Suppose I have an array of integers:

int[] numbers = [2,2,1,1,2,2,1,1,2,1]

These integers index originate from another array:

int[] primaryKey = [1, 2]

Now, an ideal situation for me would be if all numbers of the same value are next to each other in the array. e.g.

int[] numbers = [1,1,1,1,1,2,2,2,2,2]

So, in order to determine how 'good' the array is, I need to create a score the array depending on how well they are positioned, using the following rules (assume 'counter' and 'score' begin at 0):

Find first/next position of occurence of primaryKey[counter]

if next integer is the same then score = score + 3
repeat until end of repated integers found

Find next position of occurence of primaryKey[i], until end of array is reached and repeat the above. In which case...

if no more primaryKey[i] is found in array, then increment i to move onto the next element in the primaryKey array and see how badly this next integer under examination is scattered around the array using the rules above.

I have been trying to figure this out but I just can't. Can anyone help me please?
[ February 22, 2006: Message edited by: Sam Bluesman ]

Garrett Rowe
Ranch Hand
Posts: 1296
Just to be sure I understand the question , would the "score" of the "ideal array in your example be 24?

William Barnes
Ranch Hand
Posts: 986
Not sure what why you are adding 3 to the score for a good pair of numbers.

My 2 minute first cut at sudo code

Sam Bluesman
Ranch Hand
Posts: 191
24, that is correct

Sam Bluesman
Ranch Hand
Posts: 191

marc weber
Sheriff
Posts: 11343
Three questions...
• Does order matter? In particular, should {2,2,2,1,1,1} be considered as "good" as {1,1,1,2,2,2}? If I'm understanding this, these would score the same.
• Are all of these arrays the same length? If not, the score should be standardized for length.
• On a related note, do these arrays all contain the same number of unique elements? For example, do they all contain just 1's and 2's? Or might some also contain 3's, 4's, etc? This would also need to be standardized, since the first occurrence doesn't add to the score.
• Also, I'm not convinced about this approach. I understand that you want to quantify the degree to which an array corresponds to a desired order or "grouping." But I don't understand what this score actually means, because I think this depends on what you need to do with the arrays. For example, do you simply discard low-scoring arrays? Or do you need to "fix" them (which I assume has implications beyond mere sorting)? And if you're fixing them, are these scores actually representative of the amount of "work" required? In other words, what is this score really telling you?
[ February 23, 2006: Message edited by: marc weber ]