Menu
Topics Index
...
`

Sort Array Output


If you need explanation Read this topic

If you need Answer Take test on this topic

Sort Array Output
What will be the order of the array elements at the end of the program?
public class SortArray
{
    public static void main(String[] args)
    {
        int[] array = {12, 15, 11, 13, 9, 25};
        for (int i = 0; i < array.length - 1; i++)
        {
            if (array[i] > array[i + 1])
            {
                int temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
            }
        }
    }
}

A. 12, 15, 11, 13, 9, 25
B. 12, 11, 13, 9, 15, 25
C. 9, 11, 12, 13, 15, 25,
D. Throws ArrayIndexOutOfBoundsException
E. Compilation Error
Topic: Learn Arrays And Loops

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

c

Posted by Bolo Rarkings    2014-11-25 09:38:57


Answer is B
After 1st iteration, 12 15 11 13 9 25 (order is same because 12<15)
After 2nd iteration, 12 11 15 13 9 25 (15 > 11 so swap them)
After 3rd iteration, 12 11 13 15 9 25 (15 > 13 so swap them)
After 4th iteration, 12 11 13 9 15 25 (15 > 9 so swap them)
After 5th iteration, 12 11 13 9 15 25 (order is same as above step 15<25)

Posted by Shashanka Mogaliraju    2014-11-25 10:42:39


ans : b.It is like  insertion sort ....in every iteration each element is compared with the next element . 1 ite : 12>15 is the condition false nothing happens . 2nd : 15>11 condition true so swaping will done . now list is 12 11 15 13 9 25. 3rd : 15>13 cond true so swapping so now list is 12 11 13 15 9 25 .   4th : 15 > 9 true so now list is 12 11 13 9 15 25. now checks 15 > 25 con false exit from for loop.

Posted by Ananthoju Arun Chary    2014-11-25 16:03:48


ans b.                                                                        in this program Arrays can be initialized when they are declared. The process is much the same as that
used to initialize the simple types. An array initializer is a list of comma-separated expressions
surrounded by curly braces. The commas separate the values of the array elements. The array
will automatically be created large enough to hold the number of elements you specify in the
array initializer. There is no need to use new.                                                                            coming to the looping opration,it iterates length of array times.                                                     initially i=0, a[0]=12,a[1]=15.thn if condition checks a[0]>a[1]  so if faild nothing get changed.                                                                i=1, a[1]=15,a[2]=11,here 15>11 so if condition passed. thn both the values get interchanged nw a[1]=11 and a[2]=15.                               i=2,a[2]=15,a[3]=13,here also if condition passed bcz 15>13,so values get swapd.so a[2]=13,a[3]=15.                                                     i=3, a[3]=15,a[4]=9 here 15>9 so a[3],and a[4] values get swapped.nw a[3]=9,a[4]=15.           i=4,a[4]=15,a[5]=25 here 15>25 condition failed.                                                                             so at the end of the program order of the array is 12,11,13,9,15,25

Posted by Maheshwari Natarajan    2014-11-25 18:28:20


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-26 03:16:17


© meritcampus 2019

All Rights Reserved.

Open In App