Skip to main content

Posts

Java Loop Tutorial

 JAVA LOOPS  :   Loops means multiple time execution of same code written inside loop blocks. Now let's understand first FOR LOOP 1. FOR LOOP  : FOR LOOP will have 3 things. Start index A terminator condition  Increment flag. if loop start index is 1 and terminator condition is index should be less than 10 and start index incremented by 1 , then code inside loop brackets will be executed 9 times. if loop start index is 1 and terminator condition is index should be less than 10 and start index incremented by 2 , then code inside loop brackets will be executed 4 times. As mentioned FOR LOOP has three parts , start index , terminator condition and incremental value . Example :  for(int i=0 ; i < 10 ; i++ ){   // Code to be executed  } All this three operator = , < and ++ already explained in my previous post JAVA OPERATORS Please go through it if you have confusion to understand. And how this for loop is getting started and executing only till terminator condition. Java

Java Operator Tutorial

 JAVA OPERATOR  : Operator are among core concept of writing logical condition and business logic in any programming language . Simplest operators are Arithmetic Operator which we all know :  +     : Addition -     : Subtraction  *    : Multiplication  /     : Division  % : Remainder Now next is Assignment operator   = : To assign any value to any reference name of any type we use this = operator . What is variable in java already explained in my previous post JAVA BASIC OVERVIEW Example:  int input1 = 10;  String name = "My name is John Cena"; Along with this operator we can add Arithmetic Operator  to make assignment  simple  Example : For int input1 = input1 + 10 ; it can be simplified by using combination of Addition and equal to operator like  int input1 += 10;   this is same as input1= input1 + 10 ;   Same way we can simplified for Subtraction, Multiplication , Division and Remainder like : -= , *= , /= , %= Relational Operator , mainly used in case of com

Java Primitive Data types & Wrapper Class Tutorial

 JAVA PRIMITIVE TYPES AND WRAPPER CLASSES : Object class, which is stored in the java. lang package is ultimate super class of all classes. and below are the methods of Object class it means behaviour of Object class as mentioned in my previous post. Just know these methods will be more useful as we will go more deeper. Object getClass(). : returns Class class object int  hashCode() : returns the hashcode number for object being used. boolean equals(Object obj) : compares the argument object to calling object. Object clone() :  Creates copy of invoking object String  toString() : returns the string representation of invoking object void  notify() : wakes up a thread, waiting on invoking object's monitor. void  notifyAll() : wakes up all the threads, waiting on invoking object's monitor. void  wait() : causes the current thread to wait, until another thread notifies. void  wait(long,int) : causes the current thread to wait for the specified milliseconds and nanosecon

Java Overview : Basic Java Overview

  JAVA BASIC OVERVIEW :  Java is high-level, class-based, robust, platform independent and object oriented programming language. Here each keyword has specific meaning which we need to understand.     How java internally works and execute our logic ?    We will need to write a file with extension .java, in which we will be writing our logics to be executed and this should be inside a class because java starts from main method of any class which is public inside the file named same as class name.    Example :  public class FirstJavaProgram {      public static void main(String[] args) {      //our logics      } }   This code should be saved in a file named as  FirstJavaProgram.java Whenever we are writing a java class, it's basically a blue print of what class has and it can do, it means characteristics  and behaviour , just like a simple real life object like human, a human has many characteristics  and behaviours , so in java we can map char

Quick sort Implementation

  Quick sort   : In quick sort, we will consider a pivot element and divide the array so that all the lesser element will be in left side of pivot element while all the greater element will be in right side.    This process we will continue recursively until array is fully sorted.      Time complexity will beO(nlogn) but in worst case when elements are in sorted order time complexity will become O(n^2) as in each call only one element will be consider as sorted and for rest again we need to follow same process.      space complexity will be O(1). class QuickSort { public static void sort ( int [] array) { int n = array. length ; shuffle (array) ; sort (array , 0 , n - 1 ) ; } private static void sort ( int [] array , int lo , int hi) { if (hi <= lo) return; int j = partition (array , lo , hi) ; sort (array , lo , j - 1 ) ; sort (array , j + 1 , hi) ; } private static int partition ( int [] array , in

Merge sort Implementation

  Merge sort   : In merge sort we will divide the array from middle recursively  until we have only one element in sub-array. After that we will merge sub-array in ascending order.     Time complexity for merge sort will be O(nlogn) as we are splitting the array from middle so each time it's getting half so for that  it will be log(n) and while merging we are traversing the whole array so for that it will be O(n).      So after combining both  time complexity will be O(n)+O(logn)= O(nlogn)      space complexity will be O(n) as we need a extra same size array to hold the unsorted array. class MergeSort { public static void sort ( int [] array) { int [] aux = new int [array. length ] ; sort (array , aux , 0 , array. length - 1 ) ; } private static void sort ( int [] array , int [] aux , int lo , int hi) { if (hi <= lo) return; int mid = (lo + hi) / 2 ; sort (array , aux , lo , mid) ; sort (array , aux , mid + 1 ,

Insertion sort Implementation

  Insertion sort  :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper position.    Time Complexity for Insertion sort also is O(n^2).    space complexity for bubble sort will be O(1). class InsertionSort { public static void sort ( int [] array) { int n = array. length ; for ( int i = 1 ; i < n ; i++) { for ( int j = i ; j > 0 && array[j] < array[j - 1 ] ; j--) { swap (array , j , j - 1 ) ; } } } private static void swap ( int [] A , int i , int j) { int temp = A[i] ; A[i] = A[j] ; A[j] = temp ; } private static void print ( int [] A) { System. out .print( "[" ) ; for ( int i = 0 ; i < A. length ; i++) { System. out .print(A[i]) ; if (i < A. leng