Question

In: Computer Science

Can you explain in details how this loop works? int[] someNums = {90, 85, 65, 95,...

Can you explain in details how this loop works?

int[] someNums = {90, 85, 65, 95, 75} ;

a = 1;

while(a < someNums.length) {

temp = someNums [a ];

b = a - 1 ;

while(b >= 0 && someNums[ b] >temp) {

someNums [b + 1] = someNums [ b];

--b;

}

someNums [b + 1] = temp ;

++a ; }

Solutions

Expert Solution

Given loop is sorting the elemets in someNums array.

Explanation.

1) outer loop is running from a=1 to (someNums.length - 1) i.e from 1 to last index of array someNums.

2) in each iteration of outer loop element at index a is being stored in temp and b is initialized to a-1

3) Inner loop is running until b is greater than equal to 0 and element at b index is greater than temp.

4) in each iteration of inner loop, element at index b is moved to one location ahead i.e it is creating empty space for the smaller element.

5) when the control comes out of inner loop, temp is being copied to empty space as it is correct position for it.

6) after one complete iteration of outer loop, element at index a is placed at correct position.

7) when the control comes out of inner loop array is sorted.

Below is the complete program with the output of screenshot.

public class Main
{
        public static void main(String[] args) {
        int[] someNums = {90, 85, 65, 95, 75} ;

    int a = 1;
    int temp,b;

    while(a < someNums.length) 
    {
        temp = someNums [a ];
        b = a - 1 ;

        while(b >= 0 && someNums[ b] >temp) 
        {

            someNums [b + 1] = someNums [ b];
            --b;
        }

        someNums [b + 1] = temp ;

        ++a ; 
        
    }
    
    for(int i=0;i<someNums.length;i++)
    {
        System.out.println(someNums[i]);
    }
}
}

Please upvote if u like answer otherwise comment for any doubt


Related Solutions

Temperature (x) 65 70 75 80 85 90 95 100 105 Number of ice cream cones...
Temperature (x) 65 70 75 80 85 90 95 100 105 Number of ice cream cones sold per hour (y) 8 10 11 13 12 16 19 22 23 Calculate the linear correlation coefficient r, for temperature (x) and number of ice cream cones sold per hour (y). (round to 3 decimal place) Is there a linear relation between the two variables x, and y? If yes, indicate if the relationship is positive or negative. (Hint : use the Critical...
Hello sir can you explain me this code in details. void claimProcess() { int id; bool...
Hello sir can you explain me this code in details. void claimProcess() { int id; bool found = false; system("cls"); printf("Enter patient ID for which you want to claim insurrence: "); scanf("%d", & id); int i; for (i = 0; i < patientCount; i++) { if (patients[i].id == id) { found = true; break; } } if (found == false) { printf("subscriber not found\n"); return; } int numOfDaysHospitalized, suppliesCost, surgicalFee, otherCharges; bool ICU; printf("How many days were you haspitalized: ");...
int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; Create two int arrays called negative...
int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; Create two int arrays called negative and positive of length 10. Go through the given array and place the negative values into the array called negative and the positive values into the array called positive. Write code to find the smallest value in the given array and print the value out. Write code to find the largest value in the given array and print it out. Create an int array...
3. Scores on a statistics exam are given: 85 72 92 90 74 65 68 59...
3. Scores on a statistics exam are given: 85 72 92 90 74 65 68 59 74 85 63 79 81 92 95 74 88 55    Use this data to calculate the: A) mean __________ B) median ________ C) mode ________ D) range ________ E) standard deviation _________ F) find the z-score for a test grade of 90._______ G) Construct a stem and leaf display using this data. H) Construct a box and whisker plot using this data. I)...
can you explain in details how did we come up with this answer and how to...
can you explain in details how did we come up with this answer and how to answer similar.. what is the economic concept behind it A serious drought has created a shortage of food across the country. In an attempt to maximize profits, one grocery store decided to auction off its limited supply of food. Which of the following scenarios is the MOST likely to occur in this situation? A. The willingness to pay for food would rise. B. The...
can you explain in details how did we come up with this answer and how to...
can you explain in details how did we come up with this answer and how to answer similar what is the economic concept behind this NO.152 A pharmaceutical company was recently awarded a 20 year patent for a new medicine it has developed. Which of the following results could be a consequence of this patent? (Select all that apply.) A. The high price of the medicine will result in insufficient demand. B. The pharmaceutical company will charge higher prices for...
can you explain in details how did we come up with this answer and how to...
can you explain in details how did we come up with this answer and how to answer similar NO.148 Two years ago, the purchasing manager at a company spent $25,000 on a new machine that would improve production efficiency at the company. The manufacturers of the machine release an updated model that costs $35,000 and that promises to further improve production efficiency. Under what conditions should the purchasing manager upgrade to the new model? A. If the savings from the...
Describe how a negative feedback loop works in biology.
Describe how a negative feedback loop works in biology.
Java: int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; 1)     Create two int arrays called...
Java: int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; 1)     Create two int arrays called negative and positive of length 10. Go through the given array and place the negative values into the array called negative and the positive values into the array called positive. 2)     Write code to find the smallest value in the given array and print the value out. 3)     Write code to find the largest value in the given array and print it out. 4)     Create an int...
explain what a while loop is and how you can use it. Give examples based on...
explain what a while loop is and how you can use it. Give examples based on websites on how you can use a while loop to your advantage. This assignment should be a couple paragraphs long (5-8 sentences per paragraph).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT