Question

Activity 2. Suppose you have a random sequence of black, red and white marbles and want to rearrange it such that the marbles

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

Hi,
For this problem it is better to use Insertion Sort.
Let's discuss how insertion sort works:
It insertion sort we pick the each element from list and travel back(taking back that element) to the list upto present element is greater.
More clearly:

This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort. The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array).

Algorithm :

Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until last element in array(list sorted)

Pseudocode :

procedure insertionSort( A : array of items )
int holePosition
int valueToInsert
for i = 1 to length(A) inclusive do:
/* select value to be inserted */
valueToInsert = A[i]
holePosition = i
/*locate hole position for the element to be inserted */
while holePosition > 0 and A[holePosition-1] > valueToInsert do:
A[holePosition] = A[holePosition-1]
holePosition = holePosition -1
end while
/* insert the number at hole position */
A[holePosition] = valueToInsert
end for
end procedure

Example:

12, 11, 13, 5, 6

Let us loop for i = 1 (second element of the array) to 4 (last element of the array)

i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 6

i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13
11, 12, 13, 5, 6

i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move one position ahead of their current position.
5, 11, 12, 13, 6

i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one position ahead of their current position.
5, 6, 11, 12, 13
Another example with image:

Insertion Sort Execution Example 342 10 12 1 5o 2 3 4 10 12 1 5 6 ) 1 2 3 4 10 12 6 6 1 2 3 4 5 10 26 1 2 3 4 5 6 10 12

Code:

Let assume black as 1, white as 2 and red as 3

1. Python:

# Python program for implementation of Insertion Sort
# Function to do insertion sort
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
# Move elements of arr[0..i-1], that are
# greater than key, to one position ahead
# of their current position
j = i-1
while j >= 0 and key < arr[j] :
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
# Driver code to test above
arr = [2,3,1,2,2,3,2,3,2]
insertionSort(arr)
for i in range(len(arr)):
print ("% d" % arr[i])

Output: 1 2 2 2 2 2 3 3 3

2. C :

/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = { 2, 1, 3, 3, 2, 1, 2, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}

Output: 1 1 2 2 2 2 3 3 3

Note:
Time complexity of insertion sort is (n is length of list):
best : n
average: n^2
worst: n^2
Space complexity is : n

Add a comment
Know the answer?
Add Answer to:
Activity 2. Suppose you have a random sequence of black, red and white marbles and want...
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
  • 3. An urn contains 2 black, 2 red, and 4 white marbles. You draw a marble...

    3. An urn contains 2 black, 2 red, and 4 white marbles. You draw a marble at random. a) What is the sample space? b) What are all events (power Class)? c) What is the probability of each event? 4. A coin is tossed ten times and number of heads is noted. a) What is the probability that the number of heads is four? b) What is the probability of at least seven heads come? 6. X is a Binomial...

  • Suppose a jar contains 7 red marbles and 32 blue marbles. If you reach in the...

    Suppose a jar contains 7 red marbles and 32 blue marbles. If you reach in the jar and pull out one marble at random, do not replace the marble, and reach in the jar to pull out a second marble. Find the probability that both are red. Write your answer as a reduced fraction

  • Problem 3. (5-+5-+5-+10-25 points) Suppose there are two full box of marbles. The green box has...

    Problem 3. (5-+5-+5-+10-25 points) Suppose there are two full box of marbles. The green box has 10 black marbles and 40 white marbles, while red box has 25 of each. I pick a box at random, and then pick a marble at random 1. Complete the following tree diagram (filling up the nodes on the 2 layers as well as the probabilities on all of the edges) a marble chosen at random 2. We have two hypothesis: Ho: The marble...

  • Recursive backtracking: Suppose I want to burn some songs onto a 650MB CD (some people still...

    Recursive backtracking: Suppose I want to burn some songs onto a 650MB CD (some people still do!) and I have a list of song tracks of various sizes. I want to fill up as much of my CD as I can. I can use a recursive backtracking strategy to find a subset of the song tracks that optimally fills my CD (that is, with the least space left unfilled). You should recognise this problem as a version of the Knapsack...

  • please write legible and explain the answer . i do not want spss answes that do not explain the problem. A researcher conducts a study of white and black attitudes toward the police in her communi...

    please write legible and explain the answer . i do not want spss answes that do not explain the problem. A researcher conducts a study of white and black attitudes toward the police in her community. The percentage of a random sample of white respondents (N-300) who say they have a he percentage of a random sample of black favorable attitude toward the police is 51%. T respondents (N-400) who say they have a favorable attitude toward the police is...

  • Problem 1. Suppose that we take a random sequence of numbers modulo n. The proba- bility...

    Problem 1. Suppose that we take a random sequence of numbers modulo n. The proba- bility that the first k terms are all distinct is n - 1 п 2 n - (k – 1) P(n, k) = 2 п п п as we saw in class. For this problem, you will want to write a (very short) program to compute this number, but you do not need to submit it this time. (For both parts, you can submit a...

  • 3. Suppose after adding HCl to ppt 16, you do not have any black solid. What,...

    3. Suppose after adding HCl to ppt 16, you do not have any black solid. What, if anything, should you do to test for Co2 and Ni*? Qual Scheme: Group III: Co?(pink), N1" (green), Fet+ (yellow), Cr" (blue-grey), Mn(pink), AP (colorless), Zn? (colorless). Unknown B Solution: Use 10 dps of unknown sample and 10 dps of H20 and go to Procedure for Group III. Procedure for Group II: Add 2 dps NHCl soln, add 6M NHOH until just basic to...

  • IN C++!! Program 2: BACK AWAY FROM THE TV! If you sat close enough to an...

    IN C++!! Program 2: BACK AWAY FROM THE TV! If you sat close enough to an old TV, you could see individual (R)ed, (G)reen and (B)lue (or RGB) “phosphors” that could represent just about any color you could imagine (using “additive color model”). When these three color components are at their maximum values, the resulting color is white from a distance (which is amazing – look at your screen with a magnifying glass!). When all are off, the color results...

  • Suppose you have an array S indexed from 1 to n which contains n numbers, not...

    Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...

  • Monty Hall Problem - Suppose you are going to be on a game show, and you...

    Monty Hall Problem - Suppose you are going to be on a game show, and you will be given the choice of three doors: Behind one door is a car; behind the other two doors are goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your...

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
ADVERTISEMENT