Friday, March 18, 2016

Sorting: Merge Sort

Sorting is a combination of algorithms or instructions that you give in Java for the program to organize an array of numbers either from smallest to biggest or from biggest to smallest, this helps for your computer to more easily and quickly run a program which has to find a specific number. There are several ways to sort in Java there are Selection, Merge, and Insertion Sorts and each are differently efficient and are used for different reasons.

Merge Sort
public class MyMergeSort {
    private int[] array;
    private int[] tempMergArr;
    private int length;
    public static void main(String a[]){
        int[] inputArr = {45,23,11,89,77,98,4,28,65,43};
        MyMergeSort mms = new MyMergeSort();
        for(int i:inputArr){
            System.out.print(" ");
    public void sort(int inputArr[]) {
        this.array = inputArr;
        this.length = inputArr.length;
        this.tempMergArr = new int[length];
        doMergeSort(0, length - 1);
    private void doMergeSort(int lowerIndex, int higherIndex) {
        if (lowerIndex < higherIndex) {
            int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
            // Below step sorts the left side of the array
            doMergeSort(lowerIndex, middle);
            // Below step sorts the right side of the array
            doMergeSort(middle + 1, higherIndex);
            // Now merge both sides
            mergeParts(lowerIndex, middle, higherIndex);
    private void mergeParts(int lowerIndex, int middle, int higherIndex) {
        for (int i = lowerIndex; i <= higherIndex; i++) {
            tempMergArr[i] = array[i];
        int i = lowerIndex;
        int j = middle + 1;
        int k = lowerIndex;
        while (i <= middle && j <= higherIndex) {
            if (tempMergArr[i] <= tempMergArr[j]) {
                array[k] = tempMergArr[i];
            else {
                array[k] = tempMergArr[j];
        while (i <= middle) {
            array[k] = tempMergArr[i];

There are different reasons why anyone would use Merge Sort instead of other kinds:
1) it is the most efficient type of sorting for bigger arrays of numbers.
2) it offers performance just bellow Quick Sort but uses less memory space.
Merge Sort
Merge Sort Demo.
However it takes more knowledge of coding and a greater understanding of algorithms, and it takes a considerable amount of memory space, if used frequently.

Friday, March 11, 2016

Abstract and Interfaces.

There are ways to assist with inheritance and polymorphism which is done by abstract classes and interfaces these kinds of java files are used for different reasons. They both have abstract methods and those methods cannot be described like normal methods and have to be implemented in any child class of the abstract class and implemented classes.
public interface AnInteface{ // This is how to create an interface.
  public void aMathod(); // This is the way to instaciate an abstract method.
public abstract class AnAbstractClass{ // This is how to create an abstract class.
  public abstract void aMethod(); //This is another way to write an abstract method but abstract 
  // classes can also implement non-abstract methods.
 public String aString(){
  String a = "A string";
  return a;
There are two ways to implement these classes to another class:
public class NormalClass extends AnAbstractClass implements AnInterface{
 public void AMethod(){
 }// You have to incorporate the same abstract methods in a class that implements the interface or          // extends the abstract class 

Friday, March 4, 2016

Encapsulation and Polymorphism

Encapsulation is a term in Java programming and in general programming that refers to the privatization of files for classes and encapsulation of packages, there are different encapsulation key words: public, private and protected. they all do different things to your variables, classes, or methods. the public word allows to access a class, variable, or methods from one another if they are imported. The private keyword does not allow the interchange of classes, methods or variables even if they are imported. And the protected keyword only allows for methods, classes and variables to be used only within the same package. The way to import packages in any IDE from one class file is the keyword 'import' followed by a space. Ex: import java.util.*; the java.util library imports many very useful classes to the class to which you use this on, such as Random which has methods that create random numbers, or the Scanner class which reads user input from the console and gives it to a variable.
Plymorphism is a way to import classes in Java and assign them to another class as a child of that class, the best way to understand this is - Imagine that you have a dog, well that dog is an animal, but not all animals are dogs. In Java the way to assign an object as a sub-variable of another object is polimorphism.
the way to do that is to use the extends keyword it is used like this:
public class Cats extends Animals()
// all of the public methods from Animals will be usable in the Cats class.