Tuesday, May 12, 2015

Selection Sort


It is a simplest algorithm.
First look for the minimum in the array and then swap that element with the 0th position. Then find the second minimum element and swap with the index 1 element and so on.


SelectionSort ( A , n ) 
{
      for i <--- 0 to n-2
       {
            min <-- i
            for j <--- i+1 to n-1
             {
                   if ( A[i] <  A[min] )
                           min <--- j
                   
                    temp <-- A[i]
                    A[i] <--- A[min]
                    A[min] <-- temp
              }
        }
}
Read more

Bubble Sort Algorithm



In this algorithm, we scan the array from left to right multiple times. We will call each scan one pass on the array like we will first look at the 0th element then look at the index 1 and so on.

BubbleSort(A,n) // "A" is the array name and "n" is the length of array
{
       for k <---- 1 to n-1
       {
             flag <------ 0
             for i <---- 0 to n-k-1
              {
                     if ( A[i] > A[i+1] )
                      {
                            swap ( A[i] , A[i+1] )
                            flag <----- 1
                       }
               }
               if ( flag == 0 ) break
        }
}
Read more

Wednesday, February 25, 2015

Blocks of Code

Java allows two or more statements to be grouped into blocks of code, also called code blocks. This is done by enclosing the statements between opening and closing curly braces. Once a block of code has been created, it becomes a logical unit that can be used any place that a single statement can. For example, a block can be a target for Java’s if and for statements.
Consider this if statement:

if(x < y) { // begin a block
x = y;
y = 0;
} // end of block

Here, if x is less than y, then both statements inside the block will be executed.

Let’s look at another example. The following program uses a block of code as the target
of a for loop.

/*
Demonstrate a block of code.
Call this file "BlockTest.java"
*/
class BlockTest {
      public static void main(String args[]) {
               int x, y;
               y = 20;
// the target of this loop is a block

               for(x = 0; x<10; x++) {
                        System.out.println("This is x: " + x);
                        System.out.println("This is y: " + y);
                        y = y - 2;
                }
        }
}

The output generated by this program is shown here:
This is x: 0
This is y: 20
This is x: 1
This is y: 18
This is x: 2
This is y: 16
This is x: 3
This is y: 14
This is x: 4
This is y: 12
This is x: 5
This is y: 10
This is x: 6
This is y: 8
This is x: 7
This is y: 6
This is x: 8
This is y: 4
This is x: 9

This is y: 2

In this case, the target of the for loop is a block of code and not just a single statement.

Read more

Two Control Statement

1. The if Statement
The java if statement works much like the IF statement in any other language.  Its simplest form is;

if(condition) statement;

Here, condition is a Boolean expression. If condition is true, then the statement is executed. If condition is false, then the statement is bypassed.
 Here is an example:

if(num < 100) System.out.println("num is less than 100");

In this case, if num contains a value that is less than 100, the conditional expression is true, and println( ) will execute. If num contains a value greater than or equal to 100, then the println( ) method is bypassed.

Here are a few conditional expression.
Operator
Meaning
< 
Less than
> 
Greater than
==
Equal to

 Notice that the test for equality is the double equal sign.

/*
Demonstrate the if.
Call this file "IfSample.java".
*/
class IfSample {
        public static void main(String args[]) {
                int x, y;
                x = 10;
                y = 20;

                 if(x < y) System.out.println("x is less than y");
                 x = x * 2;

                 if(x == y) System.out.println("x now equal to y");
                 x = x * 2;
  
                 if(x > y) System.out.println("x now greater than y");
                 // this won't display anything

                  if(x == y) System.out.println("you won't see this");
        }
}
  
The output generated by this program is shown here:
x is less than y
x now equal to y
x now greater than y

Notice one other thing in this program. The line
int x, y;
declares two variables, x and y, by use of a comma-separated list.

2. The for Loop
Loop statements are an important part of nearly any programming language. Java is no exception.
The simplest form of the for loop is;

for(initialization; condition; iteration) statement;

The initialization portion of the loop sets a loop control variable to an initial value. The condition is a Boolean expression that tests the loop control variable. If the outcome of that test is true, the for loop continues to iterate. If it is false, the loop terminates. The iteration expression determines how the loop control variable is changed each time the loop iterates.

Simple program of for loop;
/*
Demonstrate the for loop.
Call this file "ForTest.java".
*/
class ForTest {
        public static void main(String args[]) {
               int x;
               for(x = 0; x<10; x = x+1)
                       System.out.println("This is x: " + x);
        }
}

This program generates the following output:
This is x: 0
This is x: 1
This is x: 2
This is x: 3
This is x: 4
This is x: 5
This is x: 6
This is x: 7
This is x: 8
This is x: 9














Read more

A Second Short Program

/*
Here is another short example.
Call this file "Example2.java".
*/
class Example2 {
       public static void main(String args []) {
              int num;         // this declares a variable called num
              num = 100;      // this assigns num the value 100
              System.out.println("This is num: " + num);
              num = num * 2;
              System.out.print("The value of num * 2 is ");
              System.out.println(num);
       }
}

When you run this program you will see like this output;

This is num: 100
The value of num * 2 is 200

Let's take a close look at why this output is generated.
The first new line in this program ,
  int num;         // this declares a variable called num
This line declares an integer variable called num. Java requires that variables be declared before they are used. The declaration form is,

type var-name;

Here, type specifies the type of variable being declared, and var-name is the name of the variable.

The next line is;

  num = 100;      // this assigns num the value 100

Assigns to num the value 100. In Java, the assignment operator is a single equal sign.
The next line in the program,

 System.out.println("This is num: " + num);

In this statement, the plus sign causes the value of num to be appended to the string that
precedes it, and then the resulting string is output. (Actually, num is first converted from an integer into its string equivalent and then concatenated with the string that precedes it).
Using the + operator, you can join together as many items as you want within a single
println( ) statement.

The next line of code assigns num the value of num times 2.

 num = num * 2;

Java uses the * operator to indicate multiplication.
Here are the next two lines in this program;

 System.out.print("The value of num * 2 is ");
 System.out.println(num);

The print( ) method is just like println( ), except that it does not output a newline character after each call. So, the built-in method print( ) is used to display the string "The value of num*2 is". This string is not followed by a newline. This means that when the next output is generated, it will start on the same line.











Read more