## Pages

Showing posts with label Java-Algorithms. Show all posts
Showing posts with label Java-Algorithms. Show all posts

## Thursday, February 26, 2015

### Encrypting and Decrypting Images Using BlowFish

Blowfish is a symmetric-key block cipher, designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products.

To Encrypt and Desrypt Images , follow following steps

Encrypting and Decrypting Images Using BlowFish :
1. Create SecretKey which will be used for Encryption
SecretKey secretKey = new SecretKeySpec(password.getBytes(), "Blowfish");

2. Create Cipher Instance
Cipher cipher = Cipher.getInstance("Blowfish");

3. To Encrypt, Initialize Cipher using secretKey
cipher.init(Cipher.ENCRYPT_MODE, secretKey);

## Friday, December 26, 2014

LinkedList consist of Nodes where each Node represent two things. One is Data and other is reference to Next node.

1. Create Node class containing two things. Object (basically data which node will hold) and Node which is reference to Next Node
2. add(Object data,int index) - create new Node and add that new node containing given data at specific index.

## Saturday, July 19, 2014

### Insertion Sort Algorithm using Java

Insertion Sort is simple sort algorithm. In insertion sort, we pickup element and find correct place of element where it needs to be inserted and insert that element at correct place by moving elements to right side.We will keep doing same until we have all sorted elements available on left side.

### Quick Sort Algorithm using Java

Quick Sort works on Divide and Conquer algorithm. Here, given array is divided into two parts, left and right. and both array are being sorted individually. After then we combine and will have sorted array.

How it works:
1. Pick up any element in Array. ex. middle element of array. we called this one as Pivot.
2. All elements which are smaller than Pivot are placed in one array and All elements which are larger than pivot are placed in another array
3. Using recursion sort both array using quicksort
4. Combine array as mentioned below.

### Bubble Sort Algorithm using Java

In Bubble Sort, smaller elements bubbles to top of list in ascending sorting hence it's called Bubble Sort.

How it works :
1. In Bubble Sort, You take first element and second element initially and compare both elements.
2. If second element is smaller than first element then you swipe places. how? using temp :) as below
3. You keep doing same meaning stepping through array and comparing element with pair of adjacent elements until no swapping required and you have sorted array

### Binary Search Algorithm using Java

Binary search works faster than sequential search.It search on sorted elements.
In Binary Search, Let's say You have an array and you want to search specific item in array

How it Works :
1. Initialize low=0 and high as max length of search array - 1.
2. while low<=high each item you, take middle element of array and if given searchItem is less than middle element then you need to search in array of elements array which are less than middle element. Hence, high = middle - 1

### Sequential Search Algorithm using Java

Sequential Search is simplest algorithm. You try to search given element in given collections sequentially and return only if you found element.

## Tuesday, October 08, 2013

### RSA Public Key Encryption and Private Key Decryption using Java

In Previous Post, I have discussed about What is Cryptography, What are Types of it and How we can encrypt and decrypt data using DES Algorithm in Java

Asymmetric Cryptography :
• Here, We share Public Key with others so they can encrypt data using Public Key
• We can only decrypt data using Private Key associated with Public Key. Private key is secret one and we don't share with anyone.
• Most common used algorithm is RSA ALGORITHM. named after Rivest,Shamir and Adleman

## Tuesday, September 03, 2013

### Data Encryption Decryption using DES Algorithm in Java

Encryption is process of converting plan text to cypher text using encryption algorithm and encryption Key.
Decryption is reverse process of encryption which recover original data from encrypted data using decryption key.

Here, One should understood Cryptography concept before moving into encryption and description world. It's basically making communication private - Protect Sensitive Information. Refer to wiki for more details.

Types of Cryptography :
1. Symmetric
2. ASymmetric

## Saturday, August 10, 2013

### Java Program to Find Fibonacci Series of Given Number

Fibonacci? Consider requirement that I want to display Fibonacci series of number 10.If You remember maths in school, we used to find Fibonacci number using below :

Fibonacci is sum of previous two Fibonacci numbers fn= fn-1+ fn-2

Fibonacci of 10 will be 0 1 1 2 3 5 8 13 21 34  ex. 0 1 (0+1) (1st+2nd)...

### How to Calculate Factorial of Number using Java with Recursion

Factorial? let me show you how we calculate factorial of given number. Consider requirement that I want to find factorial of number 5.

If you remembered Maths, in School we used to find factorial using below match function !
n! = n * (n-1)!

Ex. 5! = 5*(5-1)! = 5*4!
4! = 4 *(4-1)! = 4*3!
3! = 3*(3-1)! = 3*2!
2!= 2*(2-1)! = 2*1!
1! = 1*(1-0)! =  1

## Saturday, October 27, 2012

### How to check Number is Palindrome number or not using Java

Number is called Palindrome number if original number and reverse of original number are same.
ex. 46364. Just reverse the number and compare it with original number. that's it

Java Program to check number is palindrome or not :

```package com.anuj.algorithms;

/**
*
* @author Anuj Patel
*/
public class Palindrom {

protected void checkPalindrom(int num) {
int reverse = 0;
int original = num;

for (int i = 0; i <= num; i++) {
int rand = num % 10;
num = num / 10;
reverse = reverse * 10 + rand;
}
System.out.println("Original Number is - " + original);
System.out.println("Reverse Number is - " + reverse);

if (reverse == original) {
System.out.println("number is Palindrom\n");
} else {
System.out.println("number is not palindrom\n");
}
}

public static void main(String[] args) {
Palindrom palindrom = new Palindrom();
palindrom.checkPalindrom(46361);
palindrom.checkPalindrom(46364);
}
}
```

### How to Check Number is ArmStrong or not using Java

Number is called as ArmStrong number if sum of each digits's cube is equal to original number.

Ex. 153 = (1*1*1) + (5*5*5) + (3*3*3)

Please follow below Steps to check if number is armstring number or not using java :

1. Take Reminder of number
2.  Cube the reminder and add it to Sum
3.  Retrieve pending number to be evaluate and set it to Number
4.  Perform above checks until number is greater than 0.

Java Program to check number is armstrong number or not :

```package com.anuj.algorithms;

/**
*
* @author Anuj Patel
*/
public class ArmStrong {

protected void checkIsArmStrongNumber(int num) {
int sum = 0, reminder = 0;
int original = num;

while (num > 0) {
//take reminder
reminder = num % 10;

//cube the reminder and add it to sum
sum = sum + reminder * reminder * reminder;

//retrieve pending number to be eveluated
num = num / 10;
}

if (original == sum) {
System.out.println("Number " + original + " is armstrong");
} else {
System.out.println("Number " + original + " is not armstrong");
}
}

public static void main(String[] args) {
ArmStrong armStrong = new ArmStrong();
armStrong.checkIsArmStrongNumber(153);
armStrong.checkIsArmStrongNumber(154);
}
} ```