Uday's Tech Space


Merge Sort - Revisit with Fork and Join

February 23, 2015
Linear merge sort:

    private int[] mergeSort(int[] input, int fromIndex, int length) {
        if (length == 1)
            return new int[]{input[fromIndex]};
        int mid = length >>> 1;
        int[] sorted1 = mergeSort(input, fromIndex, mid);
        int[] sorted2 = mergeSort(input, (fromIndex + mid), (length - mid));
        return merge(sorted1, sorted2);
    }

    private int[] merge(int[] sorted1, int[] sorted2) {
        int[] merged = new int[sorted1.length + sorted2.length];
        int i = 0, j = 0, k = 0;
        while (i < sorted1.length && j < sorted2.length) {
            if (sorted1[i] < sorted2[j])
                merged[k++] = sorted1[i++];
            else
                merged[k++] = sorted2[j++];
        }
        if (i >= sorted1.length) {
            while (j < sorted2.length)
                merged[k++] = sorted2[j++];
        } else {
            while (i < sorted1.length)
                merged[k++] = sorted1[i++];
        }
        return merged;
    }

Fork and Join Merge Sort:

// Code to invoke fork and join merge sort
           MergeSorter mg = new MergeSorter(input, 0, input.length);
            ForkJoinPool pool = new ForkJoinPool();
            int[] output = pool.invoke(mg2);

    @Override
    protected int[] compute() {
        if (length == 1)
            return new int[]{input[startIndex]};
        int mid = length >>> 1;
        MergeSorter ms1 = new MergeSorter(input, startIndex, mid);
        ms1.fork();
        MergeSorter ms2 = new MergeSorter(input, (startIndex + mid), (length - mid));
        return merge(ms1.join(), ms2.invoke());
    }

    private int[] merge(int[] sorted1, int[] sorted2) {
        int[] merged = new int[sorted1.length + sorted2.length];
        int i = 0, j = 0, k = 0;
        while (i < sorted1.length && j < sorted2.length) {
            if (sorted1[i] < sorted2[j])
                merged[k++] = sorted1[i++];
            else
                merged[k++] = sorted2[j++];
        }
        if (i >= sorted1.length) {
            while (j < sorted2.length)
                merged[k++] = sorted2[j++];
        } else {
            while (i < sorted1.length)
                merged[k++] = sorted1[i++];
        }
        return merged;
    }

Will explain later ...

 

Counting Classes and Methods in a package

October 3, 2014
This is just a utility i created joining these blog posts:

How can I count the number of methods in a Java class ?
and 
Get All Classes Within A Package

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Counts the number of classes and methods in a package.
 */
public class CountPackageMethods {
    /**
     * Scans all classes accessible from the context class loader w...

Continue reading...
 

XML vs. JSON vs. Protocol Buffer : A Comparative Study

September 5, 2014
XML
Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.

XML Structure
XML Declaration
XML documents may begin by declaring some information about themselves, as in the following example:
<?xml version="1.0" encoding="UTF-8" ?> 
Tag
A markup construct that begins with < and ends with >. Tags come in three flavors: 
start-tags; for example: <section>
end-tags; for example: </section>
empty-...

Continue reading...
 

Interview Preperations

August 7, 2012
Collectable links ...

How Hashmap Works

GOF Design patterns Examples



Continue reading...
 

Camel: Brief Overview

February 8, 2012

According to Apache Camel site (http://camel.apache.org/): Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration.

But before jumping on to Camel, the understanding of integration patterns is must which is defined by Gregor Hohpe in his book Enterprise Integration Patterns. The same is briefly described in his website (http://www.eaipatterns.com/index.html). Also very brief summary of the same is mentioned by...


Continue reading...
 

Design Patterns

January 30, 2012

Definitions: Christopher Alexander says “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem in such a way that you can use this solution a million times over, without ever doing it the same way twice”.

As young children learn about good and evil from fairy tails, beginning software engineers learn about good design (design patterns) and bad design (anti-patterns).

History: 1987 Ward and Ke...


Continue reading...
 

Blogs I ponder to ...

September 16, 2011
Among all these years of integrating knowledge about the programming i came across some intellectual, some nice to read and some highly informative writing of the people belonging to programming community. Just sharing some of them which i still follow

1. James Gosling : http://nighthacks.com/roller/jag/
I follow James blog with all the respect for developing an successful and interesting language called Java. As i jump into Java more i found the blog as an interesting to read the man behind it...
Continue reading...
 

XML Properties

March 19, 2010
While building one of the java projects i came across properties files. They are kind of configuration files which are easy to use in java as java provide its apis. But with time i found that properties files are limited in a way like it does not have properties of a property or it could not have two or more values for the same property. For quite some time i was looking for a solution but it seems it is a less traveled path. Hence i had tried to develop something similar to properties api bu...
Continue reading...
 

About Me


Uday Shankar A curious Java Developer.
Make a Free Website with Yola.