Recursive Binary Search in java

Algorithm :
  •  Algorithm is quite simple. It can be done either recursively or iteratively: 
  •  get the middle element; 
  • if the middle element equals to the searched value, the algorithm stops; otherwise, two cases are possible: 
    • searched value is less, than the middle element. 
    • In this case, go to the step 1 for the part of the array, before middle element. searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.

QuickSort in java using divide and conquer algorithm

Algorithm :

Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.

The steps are:

  1. Pick an element, called a pivot, from the list.

Calculate age in java

Here, you can calculate the age of a person easily through the given program. This program takes your complete date of birth (year, month and day of month) and gives you the exact age which determines the years, months and days of the calculated age of the person. Following program is fully validated. Because of this you can not enter any invalid year or month or day for your date of birth.

Convert decimal to binary using Stacks in Java

How to convert decimal into binary using stacks. You all are aware of Stacks. It performs two basic operations push and pop. The push operation adds an element to the top of the list, or initializing the stack if it is empty and the pop operation removes an item from the top of the list. Using these operations, we have converted a decimal number into binary.

Mergesort in Java using divide and conquer

Merge sort is a divide and conquer algorithm. The sorting elements are stored in a collection. This collection is divided into two collections and these are again sorted via mergesort. Once the two collections are sorted then the result is combined .Merge sort will take the middle of the collection and takes then the two collection for the next iteration of mergesort. In the merging part mergesort runs through the both collections and selects the lowest of the both to insert it into a new collection.In comparison to quicksort the divide part is simple in mergesort while the merging take is complex. In addition quicksort can work "inline", e.g. it does not have to create a copy of the collection while mergesort requires a copy.

Binary search in array in java using divide and conquer

Let LIST be a list of elements that are sorted in non-decreasing order. Consider the problem of determining whether a given element x is present in the list. If x is present, we return the value j for which LIST[j]=x. If it is not in the list just return -1. The search begins by comparing x w ith the element in the middle of the list. If x equals this element, the search terminates. If x is smaller than this element, then we need only search the left half. If x is bigger than the middle element, only the right half needs to be searched

Stack Data Structure in Java

Stack data structure :

A stack is a limited version of an array.  New elements or nodes as they are often Called, can be added to a stack and removed from a stack only from one end.  For this reason, a stack is referred to as a LIFO structure (Last-In First-Out).

Operations :

The main primitive operations of a stack are:
Push : adds a new node
Pop : removes a node

Stack (Last-In First-Out)

Additional primitives can be defined:
IsEmpty : reports whether the stack is empty
Peek : return the last element in the stack

Stack Implementation in java :

Consider a stack class stores data in an array (static structure). The array reference type is Object[] which means that it can contain any kind of Java object.

The java interface is:
public interface Stack  {
public boolean isEmpty();
public Object pop();
public Object peek();
public void push(Object TheElement);


Java version of the  Stack data structure  is given below: 


Contents of Stack :
1 | 2 | 3 | 4 | 5 | 
Remove the top of stack element : 5
Return the top stack element : 4
Contents of Stack :
1 | 2 | 3 | 4 | 

Copyright © 2010-2019 Java Examples

Designed by Templateism. Built with Blogger Templates.