Wednesday, December 18, 2013

Bubble Sort in java

Let's look at the bubble sort algorithm today.
The bubble sort algorithm sorts a list of given items by comparing each element with the next element.
So let's say, we have the following list of integers {222, 3, 343, 545, 9, 99, 68, 11}, and we had to sort this list.
Bubble sort is one of the ways to do this.
Bubble sort algorithm says that, we have to take the elements one by one and compare it with the next element. If the next element is greater than current element don't do anything. However, if the next element is less than current element swap them.
We should keep doing this, until all the elements are sorted. Please find below an implementation in Java, of the bubble sort algorithm.

package org.praveen.java.learning;

public class MyBubbleSort {

    public static void main(String[] args) {

        int[] inputArray = { 1, 44, 3, 788, 54, 5, 66, 4, 2, 3, 333, 43, 34, 45,
                666, 7, 77, 8, 8877, 6, 544, 34, 33, 34, 5, 9 };
        for (int iVal = 0; iVal < inputArray.length-1; iVal++) {
            for (int jVal = iVal; jVal > 0; jVal--) {
                if (inputArray[jVal] > inputArray[jVal + 1]) {
                    int temp = inputArray[jVal];
                    inputArray[jVal] = inputArray[jVal + 1];
                    inputArray[jVal + 1] = temp;
                }
            }
        }
        for(int uuu: inputArray){
            System.out.println(uuu);
        }
    }

}

Saturday, December 14, 2013

Sort the list of strings based on the length of string

PROBLEM STATEMENT


Provide an API to sort a list of String based on the length of individual Strings.

APPROACH

- - Convert the list to TREEMAP with length of string as key and actual String as value
 RULED OUT
If the supplied list has more than one String of equal length, we will not be able to hold all the strings in the map.

- - Implement a Comparator and use in Collection.sort method
WORKED OUT

Here are the classes:


package org.praveen.java.learning;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StringLengthSorter{

    /**
     * @param args
     */
    public static void main(String[] args) {
        List strList = new ArrayList();
        strList.add("package");
        strList.add("learning");
        strList.add("import");
        strList.add("java");
        strList.add(".");
        strList.add("ArrayList");
        strList.add("util");
        strList.add("StringLengthSorter");
        strList.add("strList");
        strList.add("main");
        strList.add("void");
        strList.add("static");
        strList.add("ndtv");
        strList.add("bhasoor");
        strList.add("gg");
        strList.add("dyd");
        strList.add("jjfkd");
        strList.add("gsdjhgkd");
        System.out.println(strList);
        Collections.sort(strList, new StringLengthComparator());
        System.out.println(strList);
    }

   
}

And the COMPARATOR used above:


package org.praveen.java.learning;

import java.util.Comparator;

public class StringLengthComparator implements Comparator{

    @Override
    public int compare(String o1, String o2) {
        if(o1.length() > o2.length()){
            return 1;
        } else if (o2.length() > o1.length()) {
            return -1;
        } else {
            return 0;
        }
    }


}