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.
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.
Ordering Suppose we’re given a sequence S of n elements, each of which is colored red...
4. (10 points) Suppose we are given a sequence S of n elements, each of which is colored red or blue. Assuming S is represented by an array, give a linear-time in-place algorithm for ordering S so that all the blue elements are listed before all the red elements. What is the running time of your method?
Suppose you have an array of n elements containing three distinct keys, true, false, and maybe. Give an O(n) algorithm to rearrange the list so that all false elements precede the maybe elements, which in turn precede all true elements. You may use only constant extra space.
The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The next number is found by adding up the two numbers before it. For example, the 2 is found by adding the two numbers before it (1+1). The 3 is found by adding the two numbers before it (1+2). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called...
1. (i) [8] Suppose $s2 contains the value 6. What’s the value of $s1 after the instruction sll $s1, $s2, 5? (ii) [6] Convert jalr $s2, $t7 to machine code (binary). (iii) [6] Find the corresponding MIPS instruction to the following machine code. You are given that any immediate value is represented using 2’s complement. 0011010101010111 1111111111111111 2. [20] There is a value stored in $s0. Write a sequence of MIPS instructions that will place the absolute value of $s0...
5 matlab codes
18, Suppose I seed to fetch a mmumerical variable named hours from the user This vaniable needs to be son-orgative Ioe the emaining code in the projert to work property. so before So, we will got the vue of hours fromm the user, and if it is segative, we will tell the user that it ut be non-aegative, and reprompt for a value We will hoep doing this until the uer gots it correct Give the code...
Suppose you are given an array of n integers ai, az, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for (5, 10, 9, 13, 12, 25, 19, 30) is 5 and one such sub-array is (5, 10, 13, 19, 303. Note you may have multiple solutions for this case. Use dynamic programming to...
Suppose you are given an array of n integers a1, a2, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for {5, 10, 9, 13, 12, 25, 19, 70} is 6 and one such sub-array is {5, 10, 13, 19, 70}. Note you may have multiple solutions for this case. Use dynamic programming to...
Connect 4 is a 2 player game where each player has a set of colored tokens (red or yellow). Players take turns during which they place a single token into one of the columns of an n by m grid (where n is the number of rows and m is the number of columns. They place their token into a slot at the top of the column and it falls into the lowest unoccupied slot in that column. A player...
I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...
In terms of the programming language required, the algorithm
needs to be written in pseudocode
Dynamic Programming Consider the following problem based on the transformation of a sequence (or collection) of coloured disks Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, c. For example, the colour mapping might be 0-red. 1-yellow, 2-blue, 3-pink. , c-black For a given sequence of coloured disks e.g., ( 0,1,2,3,4...