Let's learn java programming language with easy steps

Tuesday, 17 October 2017

Selection Sort Program in Java


Selection Sort Algorithm Java

selection sort java

In the last article, We saw the example of bubble sort algorithm in java but here we are going to learn selection sort program in java with example.

Let's start selection sort program in java with output.

First questions is arise in mind what is selection sort and how it works?


What is selection sort?

Selection sort is an sorting algorithm by using section sort algorithm we can sort an array element in ascending and descending order.

In selections sort, We search for the smallest or highest elements in an array and place it to the proper or right location. We swap the current element with the next smallest or highest element.

Selection sort algorithm is better for small size of an array. It is not recommended for large size of array like other sorting algorithm in java.


Time complexity of selection sort algorithm

  • Worst case:  O(n^2)
  • Average case:  O(n^2)
  • Best case:  O(n^2)

Java Program for selection sort in ascending order

This is the simple java selections sort program in ascending order. Here we will take an unsorted array elements and then implement selection sort algorithm and arrange all the elements in ascending order.

class SelectionSortExample
{
public static void selectionSort(int array[])
{
for(int i = 0; i < array.length - 1; i++)
{
int index = i;
for(int j = i+1; j < array.length; j++)
{
if(array[j] < array[index])
{
index = j;
}
}

int smallestNumber = array[index];
array[index] = array[i];
array[i] = smallestNumber;
}
}

public static void main(String args[])
{
int array1[] = {5,4,10,2,24,8,50};
System.out.println("Before using selection sort");

for(int i : array1)
{
System.out.print(i+" ");
}
System.out.println();

//using selectionSort() method for sorting
selectionSort(array1);

System.out.println("After using selection sort");

for(int i : array1)
{
System.out.print(i+" ");
}
}
}

Output: Before using selection sort
             5 4 10 2 24 8 50
             After using selection sort
             2 4 5 8 10 24 50

This is the first selection sort program in java with output. Now moving on to java program for selection sort in descending order.


Java Program for selection sort in descending order

This is simple selection sort java descending order program. This example is same as above example but with little change. We will change " if array[j] < array[index] to array[j] > array[index]".


class SelectionSortExample
{
public static void selectionSort(int array[])
{
for(int i = 0; i < array.length - 1; i++)
{
int index = i;
for(int j = i+1; j < array.length; j++)
{
if(array[j] > array[index])
{
index = j;
}
}

int smallestNumber = array[index];
array[index] = array[i];
array[i] = smallestNumber;
}
}

public static void main(String args[])
{
int array1[] = {5,4,10,2,24,8,50};
System.out.println("Before using selection sort");

for(int i : array1)
{
System.out.print(i+" ");
}
System.out.println();

//using selectionSort() method for sorting
selectionSort(array1);

System.out.println("After using selection sort");

for(int i : array1)
{
System.out.print(i+" ");
}
}

}

Output: Before using selection sort
             5 4 10 2 24 8 50
             After using selection sort 
             50 24 10 8 5 4 2

This is simple article of selection sort algorithm with ascending and descending order in java with example.

  




Share:

Monday, 16 October 2017

Java Bubble Sort Algorithm Examples


Bubble Sort Algorithm Java

bubble sort algorithm java

Here we are going to discuss java bubble sort algorithm examples. There are many sorting algorithm including bubble sort like merge sort, quick sort, selection sort, insertion sort in java.

In this post, We will see java programs for bubble sort algorithm with ascending and descending order in java.

Let's start bubble sort example step-by-step.

First, Question is arise what is bubble sort and how it works?


What is Bubble Sort?

Bubble sort is a simple sorting algorithm in java. It is basically used to sort the elements in both cases i.e ascending and descending order.

In bubble sort, Array is traversed from first element to last elements and current element is compared with the next element. If current element is greater than the next element, it will swap.


Bubble Sort Ascending Order Java Example

This is the simple java program to arrange the digits of number in ascending order by using bubble sort. We will take integer array to traversing.

For example:

class BubbleSortExample
{
static void bubbleSort(int arr[])
{
int number = arr.length;
int temp = 0;
for(int i = 0; i < number; i++)
{
for(int j = 1; j < (number-i); j++)
{
if(arr[j-1] > arr[j])
{
temp = arr[j-1];

arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String args[])
{
int arr[] = {10, 7, 8, 50, 4, 2, 9, 17};
System.out.println("Array before using bubble sort");
for(int i = 0; i<arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();

bubbleSort(arr);
System.out.println("Array after using bubble sort");
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
}
}

Output: Array before using bubble sort
             10 7 8 50 4 2 9 17
             Array after using bubble sort
             2 4 7 8 9 10 17 50

Bubble Sort Descending Order Java Example

This is another example of bubble sort but here we are going to perform array elements in descending order with little change. Change logic  here " if(arr[j-1] > arr[j]) to if(arr[j-1] < arr[j]) ".

For example:


class BubbleSortExample

{
static void bubbleSort(int arr[])
{
int number = arr.length;
int temp = 0;
for(int i = 0; i < number; i++)
{
for(int j = 1; j < (number-i); j++)
{
if(arr[j-1] < arr[j])//change logic here for descending order
{
temp = arr[j-1];

arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String args[])
{
int arr[] = {10, 7, 8, 50, 4, 2, 9, 17};
System.out.println("Array before using bubble sort");
for(int i = 0; i<arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();

bubbleSort(arr);

System.out.println("Array after using bubble sort");
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
}
}

Output: Array before using bubble sort

             10 7 8 50 4 2 9 17
             Array after using bubble sort
             50 17 10 9 8 7 4 2

Bubble sort algorithm is a simple algorithm. Above we performed java bubble sort with ascending and descending order. This is simple sorting program in java.


Share:

Thursday, 12 October 2017

Interview Questions on Super Keyword in Java


Java Super Keyword Interview Questions

Interview Questions on Super Keyword in java

In this article, We are going to discuss the most important topic of core java interview is super keyword in java. Here we will focus on some important interview questions on super keyword in java which is frequently asked in java interviews.

Let's start basic super keyword java interview questions for 1 year experience and fresher both.


(1) What is super keyword in java?

Super keyword in java is a reference variable which is refers immediate parent or super class object. In other words, Super keyword is used to access the members of parent class.

Super keyword point to immediate parent class object. It works with object only of parent class.


(2) Write some points about super keyword in java?

There are some use of super keyword in java.
  • Super keyword can be used to refer parent class instance variables.
  • Super keyword can be used to invoke parent class method.
  • Super keyword can be used to invoke parent class constructor.

(3) Can we access parent class variables in child class by using super keyword?

Yes, We can access parent class variable in child class by using java super keyword.

For example:

class Parent
{
int a = 20;
}
class Child extends Parent
{
int a = 30;
void show()
{
System.out.println(a);//print child class value of a
System.out.println(super.a);//print parent class value of a
}
public static void main(String args[])
{
Child c = new Child();
c.show();
}
}

Output: 30
             20

You can see in the above example, There are same data member in both parent and child class. If you will not use super keyword in child class, it will print only child class value by default i.e 30 because priority goes to child. If you want to print both child and parent object values you have to use super keyword in sub class.


(4) Can we call parent class method in sub class by using super keyword.

Yes, We can access parent class method in child class by using super keyword.

For example:

class Parent
{
void display()
{
System.out.println("parent");
}
}
class Child extends Parent
{
void display()
{
System.out.println("child");
super.display();
}
public static void main(String args[])
{
Child c = new Child();
c.display();
}
}

Output: child
             parent


(5) Can we call parent class constructor in sub class constructor i.e constructor chaining by using super keyword?

Yes, We can call parent class constructor in sub class constructor by using super keyword but super keyword must be the first statement in sub class constructor.

Syntax:

class ParentDemo
{
ParentDemo()
{}
}
class Child extends ParentDemo
{
Child()
{
super();//must be first statement i.e in first line
//statement;
//statement;
}
psvm()
{}
}


(6) Can we use both "this" and "super" in constructor?

No, It is not possible in java we cannot use both this and super keyword together in java constructor because this and super should be the first statement in any java constructor.


(7) What is difference between this() and super()?

There are some difference between java this() constructor and super() constructor.

Java this()
  • It is used to access current class constructor.
  • It can be used inside another constructor of same class.
  • It can be used to remove ambiguity error when we have data members and local are same name.
For example: Remove ambiguity error

class Demo
{
int a;
int b;
Demo(int a, int b)
{
this.a = a;
this.b = b;
}
}
}

Java super()
  • It is used to access parent class constructor from sub class.
  • It can be used to only inside child class constructor.
  • It doesn't remove any ambiguity error from programs.

(8) Can we use super keyword in static method of a sub class for calling parent class method?

No, We cannot use super keyword in static methods because it belongs to the immediate parent object and static belongs to the class level.


Above all the super keyword interview questions and answers are quite useful for java interviews.





Share:

Tuesday, 10 October 2017

Top 10 Frequently Asked Java Number Pattern Programs - Part 2


Number Pattern Programs in Java

Java Number Pattern Programs

We saw some most important number pattern programs in part - 1 which is mostly asked in core java written interviews and here we will see top 10 frequently asked java number pattern programs which also asked in java interviews. This is number pattern programs in java part - 2

Let's see java number pattern programs examples one-by-one.

Java Number Pattern 1 :

1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
1 0 1 0 1 0
1 0 1 0 1 0 1

Solution :

class Pattern1
{
public static void main(String args[])
{
for(int i = 1; i <= 7; i++)
{
for(int j = 1; j <= i; j++)
{
if(j%2 == 0)

{
System.out.println(0);

}
else
{
System.out.println(1);
}
}
System.out.println();
}
}
}

Java Number Pattern 2 :

1
01
101
0101
10101
010101

Solutions :

class Pattern2
{
public static void main(String args[])
{
for(int i = 1; i <= 6; i++)
{
int n;
if(i%2 == 0)
{
n = 0;
for(int j = 1; j <= i; j++)
{
System.out.print(n);
n = (n == 0)? 1 : 0;
}
}
else
{
n = 1;
for(int j = 1; j <= i; j++)
{
System.out.print(n);
n = (n == 0)? 1 : 0;
}
}
System.out.println();
}
}
}

Java Number Pattern 3 :


00000
01000
00200
00030
00004

Solution :

class Pattern3
{
public static void main(String args[])
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
if(i == j)
{
System.out.print(i);
}
else
{
System.out.print(0);
}
}
System.out.println();
}
}
}

Java Number Pattern 4 :

101010
010101
101010
010101
101010
010101


Solution :

class Pattern4
{
public static void main(String args[])
{
for(int i = 1; i <= 6; i++)
{
int n;
if(i%2 == 0)
{
n = 0;
for(int j = 1; j <= 6; j++)
{
System.out.print(n);
n = (n == 0)? 1 : 0;
}
}
else
{
n = 1;
for(int j = 1; j <= 6; j++)
{
System.out.print(n);
n = (n == 0)? 1 : 0;
}
}
System.out.println();
}
}
}

Java Number Pattern 5 :

111111
111122
111333
114444
155555
666666

Solution :

class Pattern5
{
public static void main(String args[])
{
for(int i = 1; i <= 6; i++)
{
for(int j = 1; j <= 6-i; j++)
{
System.out.print(1);
}
for(int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}

Java Number Pattern 6 :

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Solutions :

class Pattern6
{
public static void main(String args[])
{
int number = 1;
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(number+ " ");
++number;
}
System.out.println();
}
}
}

Java Number Pattern 7 :

1 2 3
4 5 6
7 8 9

Solutions :

class Pattern7
{
public static void main(String args[])
{
for(int i = 1; i <= 9; i++)
{
for(int j = 1; j <= 3; j++)
{
System.out.print(""+i);
i++;
}
i--;
System.out.println();
}
}
}

Java Number Pattern 8 :

5432112345
4321  1234
321   123
21    12
1     1

Solutions :

class Pattern8
{
public static void main(String args[])
{
for(int i = 1; i <= 5; i++)
{
for(int j = 6-i; j >= 1; j--)
{
System.out.print(j);

}
if(i>1)
{
System.out.print(" ");
}
for(int k = 1; k <= 6-i; k++)
{
System.out.print(k);
}
System.out.println();
}
}
}

Java Number Pattern 9 :

12345
  2345
    345
      45
        5

Solutions :

class Pattern9
{
public static void main(String args[])
{
for(int i = 1; i <= 5; i++)
{
for(int j = 5-i; j < 4; j++)
{
System.out.print(" ");
}
for(int k = i; k <= 5; k++)
{
System.out.print(k);
}
System.out.println();
}
}
}

Java Number Pattern 10 :

1 2 3 4
5 6 7
8 9
10

Solutions :

class Pattern10
{
public static void main(String args[])
{
int temp = 1;
for(int i = 4; i >= 1; i--)
{
for(int j = 1; j <= i; j++)
{
System.out.print(temp++ + " ");

}
System.out.println();
}
}
}

You can check some important start pattern programs in java.

In this article, we performed most important number pattern programs in java by the help of for loop or nested loop.



Share:

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Translate