Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Suppose you have an array of N objects (as a reminder, in Java, the array wou

ID: 655166 • Letter: 3

Question

3. Suppose you have an array of N objects (as a reminder, in Java, the array would actually contain references to the objects). Each object has a key field and some other data fields. The key field ?s a string that can have only two distinct values, red and blue. Give an O(N) algorithm to rearrange the array so that all red objects precede the blue objects. (Or rather that the references to red objects precede the references to blue objects.) You may uses only constant extra space. (3 points) Note: a non-solution would be to just count the number of reds and then rewrite the key field of the first k elements with the string red and the rest with blue. This would change the nature of the objects. Imagine your objects are flowers, and one attribute is color and another is species. Now if you just over-write the color attribute you will end up with corrupted data.

Explanation / Answer

class flower{

char color[4];

char species[5];

}a1[10], temp;

  

void main()

{ for (i=0; i < n; i++)

{ for (j = 0; j< i; j++)

if(a1[j].key = "blue")

{ temp = a1[j];

a1[j] = a1[j+1];

a1[j+1] = temp;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote