Merge Sort Example in Java


package in.malliktalksjava;

/**
* @author malliktalksjava
*
* Sorts the given array using merge sort algorithm
*/
public class MergeSortExample {

private int[] array;
private int[] tempMergArr;
private int length;

/**
* @param args
*
* main method.
* Pass the array to sort method and prints the output array into console.
*/
public static void main(String args[]){

int[] inputArr = {12,25,36,95,86,21,14,52,85,32};
MergeSortExample mergeSorter = new MergeSortExample();
mergeSorter.sort(inputArr);
for(int sortedValue:inputArr){
System.out.print(sortedValue);
System.out.print(” “);
}
}

/**
* @param inputArr
*
* Checks given array is null of not, then pass it to mergeSort Method.
*/
public void sort(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {
System.out.println(“Given Array is null or does not contain any elements”);
return;
}

this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
mergeSort(0, length – 1);
}

/**
* @param lowerIndex
* @param higherIndex
*
* Sorts the array using mergesort algorithm.
*/
private void mergeSort(int lowerIndex, int higherIndex) {

if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex – lowerIndex) / 2;
// sort the left side of the array
mergeSort(lowerIndex, middle);
// sort the right side of the array
mergeSort(middle + 1, higherIndex);
// merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}

/**
* @param lowerIndex
* @param middle
* @param higherIndex
*
*
*/
private void mergeParts(int lowerIndex, int middle, int higherIndex) {

for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}
}

 

Other Useful Links:

Quick Sort Example Program in Java

Insertion Sort Example program in JAVA

Selection Sort Example Program in Java

Bubble Sort Example in JAVA

Quick Sort Example Program in Java


package in.malliktalksjava;

/**
* @author malliktalksjava
*
* Sort the given array using QuickSort Algorithm
*/
public class QuickSortExample {

private int array[];
private int length;

/**
* @param args
*
* Main Method. Calls the sort method and print the output into
* console.
*/
public static void main(String args[]) {
QuickSortExample quickSorter = new QuickSortExample();
int[] input = { 15, 42, 14, 100, 85, 1, 13, 6, 9 };
quickSorter.sortArray(input);
for (int count : input) {
System.out.print(count);
System.out.print(” “);
}
}

/**
* @param inputArr
*
* Checks given array is empty or null, if not calls quickSort
* method.
*/
public void sortArray(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length – 1);
}

/**
* @param lowerIndex
* @param higherIndex
*
* Sorts the given array
*/
private void quickSort(int lowerIndex, int higherIndex) {

int temp1 = lowerIndex;
int temp2 = higherIndex;
// calculate pivot number, here pivot is middle index number
int pivot = array[lowerIndex + (higherIndex – lowerIndex) / 2];
// Divide into two arrays
while (temp1 <= temp2) {
/**
* In each iteration, we will identify a number from left side which
* is greater then the pivot value, and also we will identify a
* number from right side which is less then the pivot value. Once
* the search is done, then we exchange both numbers.
*/
while (array[temp1] < pivot) {
temp1++;
}
while (array[temp2] > pivot) {
temp2–;
}
if (temp1 <= temp2) {
exchangeNumbers(temp1, temp2);
// move index to next position on both sides
temp1++;
temp2–;
}
}
// call quickSort() method recursively
if (lowerIndex < temp2)
quickSort(lowerIndex, temp2);
if (temp1 < higherIndex)
quickSort(temp1, higherIndex);
}

/**
* @param param1
* @param param2
*
* Exchange the numbers and set to instance array variable
*/
private void exchangeNumbers(int param1, int param2) {
int temp = array[param1];
array[param1] = array[param2];
array[param2] = temp;
}
}

 

Other Useful Links:

Bubble Sort Example in JAVA

Selection Sort Example Program in JAVA

Insertion Sort Example program in JAVA

Merge Sort Example in Java

Insertion Sort Example program in JAVA


package in.malliktalksjava;

/**
* @author malliktalksjava
* This class sort an array using Insertion sort algorithm.
*
*/
public class InsertionSortExample {

/**
* @param args
*/
public static void main(String args[]){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doInsertionSort(arr1);
for(int counts:arr2){
System.out.print(counts);
System.out.print(“, “);
}
}

/**
* @param input
* @return
*/
/**
* @param input
* @return
*/
public static int[] doInsertionSort(int[] input){

int temp;
for (int count = 1; count < input.length; count++) {
for(int count2 = count ; count2 > 0 ; count2–){
if(input[count2] < input[count2-1]){
temp = input[count2];
input[count2] = input[count2-1];
input[count2-1] = temp;
}
}
}
return input;
}
}

 

Other Useful Links:

Selection Sort Example Program in JAVA

Bubble Sort Example in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java

Selection Sort Example Program in Java


package in.malliktalksjava;

/**
* @author malliktalksjava
* @version 1.0
*
* Below class sorts the given array using SelectionSort
* mechanism and prints the output into console.
*/
public class SelectionSortExample {

/**
* @param args
*/
public static void main(String args[]){

int[] arr1 = {12,434,2,23,7,44,66,42};
int[] arr2 = doSelectionSort(arr1);
for(int var:arr2){
System.out.print(var);
System.out.print(“, “);
}
}

/**
* @param arr
* @return sorted array
*/
public static int[] doSelectionSort(int[] arr){

for (int count = 0; count < arr.length – 1; count++)
{
int index = count;
for (int count2 = count + 1; count2 < arr.length; count2++){
if (arr[count2] < arr[index]){
index = count2;
}
}

int smallerNumber = arr[index];
arr[index] = arr[count];
arr[count] = smallerNumber;
}
return arr;
}
}

 

Other Useful Links:

Bubble Sort Example in JAVA

Insertion Sort Example program in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java

Bubble Sort Example in JAVA


package in.malliktalksjava;

/**
* @author malliktalksjava
*
*/
public class BubbleSortExample {

private static int[] input = { 4, 2, 9, 6, 23, 11, 44, 0 };

public static void bubbleSort(int arr[]) {
 int count = array.length;
 int var;
 for (int i = count; i >= 0; i--) {
   for (int j = 0; j  array[j+1]) {
          swapNumbers(temp2, var, array);
      }
   }
 printNumbers(array);
 }
}

private static void swapNumbers(int var1, int var2, int[] array) {
   int temp = array[var1];
   array[var1] = array[var2];
   array[var2] = temp;
}

private static void printNumbers(int[] input) {
  for (int i = 0; i < input.length; i++) {
    System.out.println(input[i] + ", ");
  }
}
}

Other Useful Links:

Selection Sort Example Program in JAVA

Insertion Sort Example program in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java

Number series example program using JAVA


package in.malliktalksjava;

/**
* @author malliktalksjava.in
*
* This program prints the numbers in below pattern
* 1
* 123
* 12345
* 1234567
* 12345
* 123
* 1
*/
class PrintingNumberPatterns{

public static void main(String[] args){
printNumberSeries();
}

/**
* Print the numbers in assending and decending order by iterating it.
*/
private static void printNumberSeries() {
for (int i = 1; i <= 7; i += 2) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}

for (int i = 5; i >= 1; i -= 2) {
for (int j = 1; j < i + 1; j++) {
System.out.print(j);
}
System.out.println();
}
}
}

Install SSL Certificates into Java JRE


Some times it would require to install the SSL certificates into our JRE. Below are the steps that need to perform to get the certificates added into the JRE security folder.

1. Navigate to JDK\jre\bin and copy the “keytool.exe” and “jli.dll” files to the JDK\jre\lib\security directory.

2. Copy the your XXX.cer files to the JDK\jre7\lib\security directory

3. Open command prompt and navigate to the JDK\jre\lib\security directory

4. Run the following command to verify the tool is working: “keytool -list -keystore cacerts”

a. Default password to access the keystore is: changeit

5. Run the following commands to import the certificates

     keytool -keystore cacerts -importcert -alias YYY -file XXX.cer

6. If the certificate already exists then there is no need to re-import it.

 

Other Useful Links:

Javac/Java searching algorithm for other classes

Struts Internationalization Example

How to find count of duplicates in a List

How to Create a Thread Using Runnable Interface

Javac/Java searching algorithm for other classes


With this post, I would like to explain how exactly the Java/Java will search for its dependencies in the project or application level. Java Applications can be run using the command line or in the Web/Application servers. For both the scenarios will be covered as below:

When you are accessing standalone application using command prompt, below will be the search criteria steps:

Step 1: The first place that look in the directories that contains the classes that come with Java SE.

Step 2: The classpath that declared as command line options for either Java/Javac. Since the classpath declared in command line options override the classpath declared in command line options persists only for the length of the invocation.

Step 3: Looks for the the default classpath that declared as System Environment Variables.

When you are accessing the application from Web or application servers, below will be the search criteria steps:

Step 1: The first place that look in the directories that contains the classes that come with Java SE.

Step 2: If the dependencies not able to identify in the JRE lib folder, then it will be looked into the Application lib folder.

Step 3: If the dependencies not able to fine in above two steps, then it will be looked into Web/App server lib folder.

 

Other Useful Links:

Differences between Object and Instance
Difference between Abstract Class and Interface:
Threads Interview questions in Java
Access Specifiers in Java
Avoid nested loops using Collection Framework in Java

Example Program – Armstrong Number


Below sample programs finds whether the given number is an Armstrong Number or not.

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Few more Examples for the Armstrong numbers are : 153, 371, 9474, 54748.

 

package in.javatutorials;

/**
* @author malliktalksjava.in
*
*/
public class ArmstrongNumber {

/**
* @param inputNumber
* @return boolean true/false will be return
*/
public static boolean isArmstrong(int inputNumber) {
String inputAsString = inputNumber + “”;
int numberOfDigits = inputAsString.length();
int copyOfInput = inputNumber;
int sum = 0;
while (copyOfInput != 0) {
int lastDigit = copyOfInput % 10;
sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
copyOfInput = copyOfInput / 10;
}
if (sum == inputNumber) {
return true;
} else {
return false;
}
}

/**
* @param args
*/
public static void main(String[] args) {
int inputNumber = 153;
System.out.print(“Enter a number: “);
boolean result = isArmstrong(inputNumber);
if (result) {
System.out.println(inputNumber + ” is an armstrong number”);
} else {
System.out.println(inputNumber + ” is not an armstrong number”);
}
}
}

 

Example program to reverse a Number in Java


 

package in.javatutorials;

public class ReverseNumber {

public static void main(String[] args) {
System.out.println(“The reversed number is ” + reverse(1234));
}

public static int reverse(int input) {
int result = 0;
int rem;
while (input > 0) {
rem = input % 10;
input = input / 10;
result = result * 10 + rem;
}
return result;
}
}