Techhgeekz ™

Thursday 17 July 2014

What is Big O notation ?

                                               Big O notation 

Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm.

Anyone who’s read Programming Pearls or any other Computer Science books and doesn’t have a grounding in Mathematics will have hit a wall when they reached chapters that mention O(N log N) or other seemingly crazy syntax. Hopefully this article will help you gain an understanding of the basics of Big O and Logarithms.

As a programmer first and a mathematician second (or maybe third or fourth) I found the best way to understand Big O thoroughly was to produce some examples in code. So, below are some common orders of growth along with descriptions and examples where possible.

O(1)

O(1) describes an algorithm that will always execute in the same time (or space) regardless of the size of the input data set.
bool IsFirstElementNull(String[] strings)
{
if(strings[0] == null)
{
return true;
}
return false;
}

O(N)


O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set. The example below also demonstrates how Big O favours the worst-case performance scenario; a matching string could be found during any iteration of the for loop and the function would return early, but Big O notation will always assume the upper limit where the algorithm will perform the maximum number of iterations.
bool ContainsValue(String[] strings, String value)
{
for(int i = 0; i < strings.Length; i++)
{
if(strings[i] == value)
{
return true;
}
}
return false;
}

O(N2)


O(N2) represents an algorithm whose performance is directly proportional to the square of the size of the input data set. This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(N3), O(N4) etc.
bool ContainsDuplicates(String[] strings)
{
for(int i = 0; i < strings.Length; i++)
{
for(int j = 0; j < strings.Length; j++)
{
if(i == j) // Don't compare with self
{
continue;
}

if(strings[i] == strings[j])
{
return true;
}
}
}
return false;
}

O(2N)


O(2N) denotes an algorithm whose growth will double with each additional element in the input data set. The execution time of an O(2N) function will quickly become very large.

Friday 11 April 2014

Spread dot Operator in Groovy

After a long gap i'm gonna share one new feature in groovy ...its all about invoking the size method on each array element  

 Before going into Groovy...wanna to share some new pretty cool features of groovy over java

Groovy is known as Java scripting language and a lot of groovy users are taking advantage of its flexible nature. Groovy is better known as a new age advanced substitute of Java, or we can say it a better Java. Groovy takes advantage of static Java compiler and is purely a JVM based language, it generates bytecodes like Java and provides Java stability and trust in a better and flexible way.

Groovy is a JVM based dynamic language that runs on the Java Compiler. Groovy contains Java like syntax and design pattern most of the Java code is also valid in groovy but not all, rather groovy is much more dynamic and a new version of Java in a better and productive manner.

Although Java is a very good and widely used programming language but implementing some tasks like string manipulation and file handling is a a great pain in Java. Using Groovy one can implement and code difficult stuffs in a very dynamic and easy way. Groovy files have an extention of '.groovy' and unlike Java we do not need to put a semicolon after each programming statement. If you knows Java than its very easy to learn and implement groovy in your programming stuff. 

Groovy is designed as much as familiar with Java and tried to make general purpose tasks easy.

for more info :- http://www.beingjavaguys.com/2013/02/what-is-groovy-java-vs-groovy.html

 Wogay Let's dive into Groovy's pretty cool features:-


 Use of spread-dot operator:-

def animals = ['ant', 'buffalo', 'canary', 'dog']    // Array declaration in groovy 'll be like this [] instead of {} in java
assert animals.size() == 4  // assert keyword-Enables you to test your assumptions about your program
assert animals*.size() == [3, 7, 6, 3]       //size of each array element is given in the array

Moreover :- Semicolon is not needed in groovy :) :)

Thursday 9 January 2014

Facebook buys Bangalore based Indian Start Up

Facebook has acquired year-old Indian startup Little Eye Labs to help concentrate on mobile development for the social media platform.

As part of the acquisition, Little Eye Labs will relocate to Menlo Park, California, the home of Facebook's headquarters.

"From there, we'll be able to leverage Facebook's world-class infrastructure and help improve performance of their already awesome apps. For us, this is an opportunity to make an impact on the more than 1 billion people who use Facebook," the startup wrote on its site.

The transaction also means that Little Eye Labs has stopped accepting any new signups to its service.

Its core business revolves around developing performance analysis and monitoring tools for Android developers. This includes tracking and monitoring how apps and processes consume handset resources such as power, memory, storage; the impact of processes and active hardware like GPS and Wi-Fi and events and logs at the application and system level.

Although no new customers can sign up for these features, Little Eye Labs has promised a free version of its existing offering to its current customers, which will work up until June 30.
A source familiar with the deal told Tech Crunch that the acquisition was made in the range of $10 million to $15 million.

Monday 30 December 2013

Generics in Java ..!

Generics :-

Enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs. The difference is that the inputs to formal parameters are values, while the inputs to type parameters are types.

Hope this Quote would resolve yur  Fuzzy about Generics ..! ;) 

Code that uses generics has many benefits over non-generic code:

Stronger type checks at compile time.

A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety.

 Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.

For in Depth about Generics visit :-

Monday 2 December 2013

String Reverse Using Recursion in Java!!

class main

{

public static void main(String args[])

{

string str="hey";

 reverseStr = reverseRecursively(str);

public static String reverseRecursively(String str) 

{
        //base case to handle one char string and empty string
        if (str.length() < 2) 

{
            return str;

  }
        return reverseRecursively(str.substring(1)) + str.charAt(0);
 }
  System.out.println("Reverse String in Java using Recursion: " + reverseStr);

}


Tuesday 29 October 2013

why the string is immutable?

String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.

The following code will create only one string object in the heap.
String string1 = "abcd";
String string2 = "abcd";



Monday 30 September 2013

Calling a constructor from another constructor in same class using "this" keyword:-

class Flower

{

int num = 0;

 String s = new String("null");

 Flower() //Default Constructor

 { 

 this(1000,"Hello");     // calling constructor with 2 parameters

 System.out.println("Danger ! Inside the default constructor");
   }
  Flower(int x, String y)

 {

//constructor call must be the first statement in a constructor !!

 this(500);          //call to integer parametered constructor

  //! this(s);        // Can’t call two constructors within a unit!
 
  this.num = x ;      //simple num = x; is also right

    s = y;

   System.out.println("Danger ! Inside the double barelled constructor");

   System.out.println("values >>  [ "+num+" | "+s+" ]");
    }
   Flower(int x) {
   this("Buhaha");     //call the constructor with string parameter
   this.num = x ;      //simple num = x; is also right 
   System.out.println("Danger ! Inside integer constructor");
   System.out.println("values >>  [ "+num+" ]");
    }
   
    Flower(String y) {
    this.s = y ;    
    System.out.println("Danger ! Inside String constructor");
    System.out.println("values >>  [ "+s+" ]");
    }
}
public class ConstrInThis {
    public static void main(String[] args) {
        Flower rose = new Flower();
    }
}
 

Rules to be followed while using "this" keyword for calling another constructor in same class:-

You can call one constructor from another in the same class, or call the super class, with the following restrictions:

  1. It has to be the first line of code in the calling constructor.
  2. It cannot have any explicit or implicit reference to this. So you cannot pass an inner class (even an anonymous one if it references any instance methods), or the result of a non-static method call, as a parameter.