Latest Updates

Insertion Sort in Java

Posted By: Achchuthan Yogarajah - 3:01 PM
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:
How Insertion Sorting Works (http://www.studytonight.com/data-structures/insertion-sorting)

Java Source Code:



Output:

Before Sorted: 15 12 1 9 25 7 14 12 10 
After Sorted: 1 7 9 10 12 12 14 15 25 

Selection Sort in Java

Posted By: Achchuthan Yogarajah - 10:38 PM
The idea of algorithm is quite simple. The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

Let us see an example of sorting an array to make the idea of selection sort clearer.

Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort.
Example 1 (http://www.algolist.net/Algorithms/Sorting/Selection_sort)

Java Source Code:



Output:

Enter the array size:
3
Enter the array elements:
4
12
1
Before Sorted :Array Elements:
4 12 1
After Sorted :Array Elements:
1 4 12

Palindrome program using stack and queue in java

Posted By: Achchuthan Yogarajah - 10:03 PM
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers. Famous examples include "A man, a plan, a canal, Panama!", "Amor, Roma", "race car", "stack cats", "step on no pets", "taco cat", "put it up", "Was it a car or a cat I saw?" and "No 'x' in Nixon"

Implement the Dynamic Programming-Rod Cutting in Java

Posted By: Achchuthan Yogarajah - 7:57 PM
Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)

Bloom filter to test whether a given word is in a set or not in Java using MD5 hashing

Posted By: Achchuthan Yogarajah - 7:26 PM
There are many circumstances where we need to find out if something is a member of a set, and there are many algorithms for doing it. For example a spell checker checks to see whether a given word is in the dictionary. Holding 250,000 words in memory for a spell checker might be too big an overhead if your target environment has low memory.

Find the MD5 hash value of a string in java

Posted By: Achchuthan Yogarajah - 6:59 PM
Here is the example is show quick way to calculate the MD5 hash of a string. I know things with Java have to be well structured and since MD5 is a hashing algorithm it is fairly logical to be in the security package. On the other hand i think a quick md5 method should be in the String class but that’s just my humble oppinion. Anyways here is how it’s done the Java way.

Convert roman numerals to decimal in java

Posted By: Achchuthan Yogarajah - 7:32 PM
Read a roman number and convert that into decimal. For such a problem we have to find a suitable solution. we need to write a method to read the Roman number and a converter. there is a  common patterns in Roman numbers and find a solution to convert.
For example , " III "means 1+1+1=3 , " V " means 5 " IV " means 4 how ?
let see , First read string and check from back to front .so now we have first " V " and " I " .now check last character is grater other one then " - " operator else " + "operator   ( if V > I then 5 - 1 = 4 )

Sparse Matrix Operations in Java

Posted By: Achchuthan Yogarajah - 11:56 AM

In the subfield of numerical analysis, a sparse matrix is a matrix populated primarily with zeros (Stoer & Bulirsch 2002, p. 619) as elements of the table. By contrast, if a larger number of elements differ from zero, then it is common to refer to the matrix as a dense matrix. The fraction of zero elements (non-zero elements) in a matrix is called the sparsity (density).

Basic Queue program in java

Posted By: Achchuthan Yogarajah - 9:56 PM
The Queue.java program features a Queue class with insert(), remove(), peek(), isFull(), isEmpty(), and size() methods. The main() program creates a queue of five cells, inserts four items, removes three items, and inserts four more. The sixth insertion invokes the wraparound feature. All the items are then removed and displayed.

Find all anagrams of a word in Java

Posted By: Achchuthan Yogarajah - 10:22 PM
Finding an algorithm to answer this question may seem challenging because finding all the different permutations of a string is something that you just do naturally without really thinking about it. But, try to figure out what algorithm you are implicitly using in your mind whenever you write down all the different permutations of a string. Let’s use the word “dogs” as an example and see what different permutations we get. Here is what comes up when we list out all the possible permutations of the letters in “dogs”: 

Queue data structure using Array in Java

Posted By: Java Examples - 3:52 PM
A queue is a linear list of elements in which deletions can take place only at one end, called the front of the stack, and insertion can take place only at the other end, called the rear. The term FRONT and REAR are used in describing a linear list only when it is implanted as queue. Queues are also called First in First out (FIFO) Lists. This makes the queue a First-In-First-Out (FIFO) data structure. 
In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once an element is added, all elements that were added before have to be removed before the new element can be invoked.

Generic Type in Java

Posted By: Java Examples - 10:16 PM
The term generics here means parameterized types. Parameterized types are important because they enable you to create
classes, interfaces, and methods in which the type of data upon which they operate is specified as a parameter. 
Using
generics, it is possible to create a single class, for example, that automatically works with different types of data. A class,
interface, or method that operates on a parameterized type is called generic, as in generic class or generic method.

Data%20Structures

Sorting

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .