1.When we run the program on this input using our altered version of sortArray above, what value does the newly inserted line print? (Be sure to remember that array positioning is 0-based, so that the very first B is actually at position 0).
2.Now suppose that we give the program this input:
\"BBBGGGG\"
Notice that the String \"BBBGGGG\" is already sorted.
When we run the program on this input using our altered version of sortArray above, what value does the newly inserted line print? (Be sure to remember that array positioning is 0-based, so that the very first B is actually at position 0).
3.Now suppose that we give the program this input:
\"GBBBBB\"
When we run the program on this input using our altered version of sortArray above, what value does the newly inserted line print? (Be sure to remember that array positioning is 0-based, so that the very first B is actually at position 0).
4.Now suppose that we give the program this input:
\"BBBBB\"
Notice that the String \"BBBBB\" is already sorted properly. When we run the program on this input using our altered version of sortArray above, what value does the newly inserted line print? (Be sure to remember that array positioning is 0-based, so that the very first B is actually at position 0).
Solution
We are printing value of beginP after outer while loop
There are two condition to come out from outer while loop:
a. value of beginP > endP
or  b. value of beginP >= length of array
And, array will be sorted after outer loop
So, at that time (after outer while loop completion)
Value of beginP :
1. (index of last B) + 1
or 2. equal to length of array - 1
which ever will be minimum
1.
Input: BBBGG
here , initial value of beginP = 0;
length of array = 5
index of last B = 2
Hence, Output: 3
2.
Input: BBBGGGG
here , initial value of beginP = 0;
length of array = 7
index of last B = 2
Hence, Output: 3
3.
Input: GBBBBB
here , initial value of beginP = 0;
length of array = 6
index of last B = 5
Hence, Output: 5
4.
Input: BBBBB
here , initial value of beginP = 0;
length of array = 5
index of last B = 4
Hence, Output: 5
.