Wednesday 4 January 2017


Selection Sort : Java Program

Selection Sort is one of sorting techniques to get arranged the numbers in ascending order. 

Selection Sort method:

We will take the following list of elements.

10  30  15  6  4  1  8

Logic for finding the biggest number from the list:

Here we have 7 elements to be sorted.  In the selection sort method, we will pick up biggest element from the list.  For this we assume that first element is biggest number. Then compare the first number with the second number in the list.  Here 10 is the first number and 30 is the second number. From these two 30 is the biggest number. Now we can treat 30 is the biggest number. Go for comparison with the biggest number and next number 15. Again we get 30 is the biggest number. Now compare biggest number 30 with the next number 6. Among these two 30 is the biggest number. Compare this biggest number with the next number 4. Now compare 30 with the next number 1. Again we get 30 is the bigger number. Now compare 30 with the last number 8. We get the 30 is the largest number from the list.

The whole paragraph above gives that, finding of largest number from the given list.

In the above list 30 is the biggest number and it is located at 2 position. After finding the biggest number from the list,  we will change the positions of biggest number with the last number. That is we have to change positions of 30 and 8. After exchanging the positions,  the list look like this

10  8  15  6  4  1  30  -------> I Pass

This is called first pass. During the first pass we can get one element as sorted.  Apply the same logic to the remaining 6 elements.

Among the 6 elements 15 is the biggest element.  Hence we have to swap the positions of biggest number 15 and last number 1. After doing this, the list look like this.

10  8  1  6  4  15  30  --------> II Pass

This is called second pass.

Apply the same logic for the remaining 5 elements. The III Pass looks like this after swapping 10 and 4 positions

4  8  1  6  10  15  30 --------> III Pass

Among the remaining four elements, 8 is the biggest number and 6 is the last number. Therefore, we have to exchange these positions we get the following the IV pass.

4  6  1  8  10  15  30 ---------à IV Pass

We have here 3 elements remaining. Among these 6 is the largest one and last number is 1. We can swap these elements to get the V Pass and it looks like the following

4  1  6  8  10  15  30 ---------à V Pass

We have here 4 is the largest element and 1 is the last element. So we have swap these two to get the VI Pass. The list looks like this.

1  4  6  8  10  15  30 --------------à VI Pass

You can observe from the VI Pass, all the elements got arranged in the ascending order.
If elements are 7, then 7-1=6 passes require to sort the numbers in ascending order.  In general, if n elements are in the list, then n-1 passes are required to get the numbers in sorted order.

This type of sorting technique is called selection sort method.


Java Program for Selection Sort:

class Sele
{
       public static void main(String args[])
       {
           int a[]={10,30,15,6,4,1,8}; 

           int i,b,id,j,n;
           n=a.length;
           System.out.println(" Elements before sorting");
           for(i=0;i<n;i++)
             System.out.print(a[i] + " ");
           for(i=n-1;i>=0;i--)
            {
                 b=a[0];
                 id=0;
                 for(j=1;j<i+1;j++)
                   {
                        if(a[j]>b)
                          {
                               b=a[j];
                               id=j;
                          }
                    }
                a[id]=a[i];
                a[i]=b;
           }
         System.out.println();
         System.out.println("Elements after sorting");
         for(i=0;i<n;i++)
          System.out.print(a[i] + " ");
      }
}
output:

 Elements before sorting
10  30  15  6  4  1  8

Elements after sorting

1  4  6  8  10  15  30

Saturday 24 December 2016

Insertion Sort

Watch the video.  I have explained about Insertion Sort using Java language.




Saturday 29 October 2016

Java Language - Decision making branching and looping

Decision making branching and looping in Java:

As we know that the instructions, which we are written in Java language, are executed in sequential order when we compile the program. If we want to execute certain instructions based on the condition, then we apply either branching or looping in the program.

Decision making - branching: we will follow the three Java statements in this regard. The three statements are
i) if statement   ii) switch statement iii) the ?: operator

i) The if statement takes three formats. a) simple if statement b) if - else statement c) nested if statement d) else-if ladder

a) Simple if statement

Systax:
                    if(condition)
                    {
                         statement-1;
                         statement-2;
                             ....
                         statement-n;
                     }
                    statement-k;



Control checks the condition first.  If condition is true, then control enters into if statement and statements which are kept in brace brackets executed once and after that, controls comes out of the if statement.
Ex.
if (n%2==0)
{
                                System.out.println(“ Number is even”);
}

If n value is 20, then condition becomes true then the output statement is executed and message “Number is even”  will be displayed.

b) Syntax: if – else statement
if (condition)
{
                                    Statement-1;
Statement -2;
Statement-n;
}
else
{
Statement-a;
Statement -b;
Statement-z;
}

Execution process:

If condition is true, the control enters into the if statement block and statement-1, statement-2, .. Statement- n are executed and else part is vomited from the execution process. That means  statement-a, statement-b … statement-z are not included in the execution.  On the other hand, if condition is false, else part statements are executed and the block mentioned before the else are not participated in the execution process.  

Ex:
                                if(n%2==0)
   {
      System.out.println(“number is even”);
   }
         else
                {
                    System.out.println(“number is odd”);
                }


If n value 20, the message “number is even” displayed. If n value is 21 the message “number is odd” is displayed.




c) Systax: nested if

if(condition-1)
         {
                                if(condition-2)
                                 {
                                    Statement-1;
                                    Statement-2;
                                         
                                    Statement-3;
                                }
                        }


Nested if statements are allowed in Java programming.  That means, we have to able to use one if statement within the other if statement. Depending upon the requirements of the program we are able to use any number of if statements within the other if statement.

Ex:

class A
{
   public static void main(String args[])
{
      int x = 30;
      int y = 3;
      if( x == 30 )
            {
                 if( y == 3 )
                      {
                          System.out.println("Number 30 is completely divisible by 3);
                     }
           }
   }
  }

The condition x==30 is true here, then control enters into the first if statement block and checks condition for true or false value, if it is true (in present case It is true), the block of statements are executed. Here in this particular example, the message “Number 30 is completely divisible by 3” is displayed as two conditions are true.






d) Syntax: else if else ladder

if(condition-1)
{
     Statement-1;
     Statement-2;                when condition-1 true only these statement are executed
     
     Statement-n;
}
 else if (condition-2)
 {
      Statement-1;
                  Statement-2;                When condition-2 true only these statements are executed
                   ….
                  Statement-n;
  }
. . .
else if (condition-n)
{
      Statement-1;
                  Statement-2;                 When condition-n true only these statements are executed
                   ….
                  Statement-n;
}
else
{
   Statement-1;
               Statement-2;                    All above conditions get false, then these statements are executed
                   ….
               Statement-n;
              }


Ex: 

class ladder
{
     public static void main(String args[])
     {
         
         int demo=3;

         if(demo==1)
         {
             //This block will be executed only if "demo" is equal to 1
             System.out.println("Hyderabad");
         }
         else if(demo==2)
         {
             //This block will be executed only if "demo" is equal to 2
             System.out.println("Warangal");

         }
         else if(demo==3)
         {
             //This block will be executed only if "demo" is equal to 3
             System.out.println("Nalgonda");
         }
         else
         {
             System.out.println("No Place Found");
         }
     }
}


The output for this program is Nalgonda  


ii) Switch Statement

Sytax:


switch(expression) {
 
   case label1  :
                                   statement-1;
                                   statement-2;
                                          ….
                                   Statement-n;
                                    break; 
                    
   case lable2  :
                                   statement-1;
                                   statement-2;
                                           ….
                                   Statement-n;
                                   break; 
  
                                           . . .
 
   default : 
                                     statement-1;
                                     statement-2;
                                          ….
                                     Statement-n;
                                     Break;
 
            }

Execution Process:

Firstly expression is evaluated and depending on the result, concerned statements are executed.   That means, if expression value is equal to label1, then the statements mentioned under case label1 are executed and control comes out of the switch statement. If expression value is not met any one of label value then the statements mentioned under default are executed.

Ex.
 /* this is not complete program. It is partial one */


d=b*b-4*a*c;
if(d<0)
         k=1;
       else
        {
            if(d==0)
            k=2;
        else
        k=3;

switch(k)
{
case 1: System.out.println("roots are imaginary");
               break;
case 2: System.out.println("roots are equal");
               x1=-b/(2*a);
               System.out.println(x1 + “  “ + x1);
               break;
case 3: System.out.println ("roots are unequal");
              x1=-b+sqrt(d)/(2*a);
              x2=-b-sqrt(d)/(2*a);
              System.out.println(x1 + “  “ + x2);
              break;
defualt: System.out.println(" You have entered wrong values");
                 break;
}




iii) ?: operator (Conditional operator)

Systax:


expression 1? expression 2: expression 3;
 
Execution Process:
 
If expression-1 is true then expression-2 is executed and expression-3 is ignored.  If expression-1 is 
false then expression-2 is ignored and expression-3 is executed.  It is some what like if – else statement.
 
Ex:
 
n%2==0 ? System.out.println(“No. is even”) : System.out.println(“No. is odd”);


when the result of n%2==0 is true, the message "No. is even" is displayed or when the result of n%2==0 is false, then the message "No. is odd" is displayed.