Question

Ordering Suppose we’re given a sequence S of n elements, each of which is colored red...

Ordering

Suppose we’re given a sequence S of n elements, each of which is colored red or blue. Assume S is represented as an array. Give an in-place method, Blue-Red(S, n), in a pseudo code for ordering S so that all the blue elements are listed before all the red elements.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In - place method = where input is overwritten by output during algorithm execution.

Let's consider,

1 represents all red coloured elements in the sequence and

0 represents all blue coloured elements in the sequence.

S = sequence array

n = size of array

// Pseudocode for ordering the sequence such that all blues are listed before all reds

Blue-Red( S, n )

{
countBlue = 0; // count number of elements representing blue colour i.e counting zeroes(0).

for ( index = 0; index < n; index++)

{ // count Blues in the sequence
if ( S[index] == 0 ) :

then countBlue++;

}

for ( index = 0; index < countBlue; index++)

{ // fill the sequence with 0 upto its count to represent blue elements in beginning

S[index] = 0;

}

for ( index = countBlue; index < n; index++)

{ // fill the sequence with 1 in remaining portion for red elements

S[index] = 1;

}

}

This way sequence S would contain blue coloured elements before the red ones.

Add a comment
Know the answer?
Add Answer to:
Ordering Suppose we’re given a sequence S of n elements, each of which is colored red...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT