Skip to main content

Posts

Showing posts from January, 2017

Image Gallery Using Flickr

Hi friends, have a great day. Now I just explain how to use Flickr.

Flickr is one of largest media library in the world, you can store your photos & videos in this application.

Both web version and mobile version are available.

API details: https://www.flickr.com/services/api/

You can access the flickr API by create the app, get the api_key and secret key.

You can able to show the public or your own photo catalogues.

Public feeds are available in

https://www.flickr.com/services/feeds/docs/photos_public/

I create one simple application to access the public photos using Flickr API.

You can download or clone in Github

Algorithm - Heap Sort

Heap Sort
Heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region.

It's an In-Place algorithm, but It's not a stable sort.

Solution

public class HeapSort {
    private static int num;
    public static void sort(int array[]) {
        doSort(array);
        for (int i = num; i > 0; i--) {
            swap(array, 0, i);
            num = num - 1;
            maxHeap(array, 0);
        }
    }
    public static void doSort(int arr[]) {
        num = arr.length - 1;
        for (int i = num / 2; i >= 0; i--) {
            maxHeap(arr, i);
        }
    }
    public static void maxHeap(int array[], int i) {
        int left = 2 * i;
        int right = 2 * i + 1;
        int max = i;
        if (left <= num && array[left] > array[i]) {
  …

Algorithm - Merge Sort

Merge Sort
Merge sort is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output.

Solution
public class MergeSort {

    static int[] tempArr;

    public static void main(String a[]) {

        int[] inputArr = {11, 8, 34, 5, -4, 19, 0, 25, 97, 83, 8};
        sort(inputArr);
        for (int i : inputArr) {
            System.out.print(i);
            System.out.print(" ");
        }
    }

    static void sort(int array[]) {
        tempArr = new int[array.length];
        doMergeSort(array, 0, array.length - 1);
    }

    static void doMergeSort(int[] array, int lowerIndex, int higherIndex) {

        if (lowerIndex < higherIndex) {
            int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
            // Below step sorts the left side of the array
            doMergeSort(array, lowerIndex, middle);
            // Below step sort…

Algorithm - Quick Sort

Quick Sort
Quicksort  is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. it is still a commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than merge sort and heapsort.

Solution
public class QuickSort {

    static void sort(int[] inputArr) {

        if (inputArr == null || inputArr.length == 0) {
            return;
        }
        doQuickSort(inputArr,0, inputArr.length - 1);
    }

    static void doQuickSort(int[] array, int lowerIndex, int higherIndex) {

        int i = lowerIndex;
        int j = higherIndex;
        int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
        while (i <= j) {
            while (array[i] < pivot) {
                i++;
            }
            while (array[j] > pivot) {
                j--;
            }
            if (i <= j) {
                exchangeNumbers(array,i, j);
                i++;
                j--;
            }
        }
    …

Algorithm - Insertion Sort

Insertion Sort
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It's not much efficient in large sets of data.

Solution

public class InsertionSort {

    public static void main(String arg[]) {
        int[] arr1 = {84, 43, 17, 65, 6, 19, 83, 4};
        int[] arr2 = doSort(arr1);
        for (int i = 0; i < arr2.length; i++) {
            if (i > 0) {
                System.out.print(", ");
            }
            System.out.print(arr2[i]);
        }
    }

    public static int[] doSort(int[] array) {

        int temp;
        for (int i = 1; i < array.length; i++) {
            for (int j = i; j > 0; j--) {
                if (array[j] < array[j - 1]) {
                    temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                }
            }
        }
        return array;
    }
}

Result: 4, 6, 17, 19, 43, 65, 83, 84

Algorithm - Selection Sort

Selection Sort
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.

The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right.

Algorithm is not suitable for large data sets.

Solution:

public class SelectionSort {

    public static int[] sortNumbers(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int index = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[index]) {
                    index = j;
                }
            }

            int smallerNumber = arr[index];
            arr[index] = arr[i…

Algorithm - Bubble sort

Now we are going to look up some algorithms.

Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order

Example in Java:

public class BubbleSort {

    public static void bubble_sort(int arr[]) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    // swap temp and arr[i]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    private static void swapNumbers(int i, int j, int[] array) {
        int temp;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    private static void printNumbers(int[] input) {
        int n = input.length;
        for (int i = 0; i < n; ++i) {
            if (i != n) {
                System.out.print(input[i] + &quo…

Print the numbers in Triangle or Pyramid Shape.

Here is the task, want to print the numbers in triangle or pyramid shape.

Like this,

        1
     2    2
   3   3    3
4   4    4    4


Solution:

import java.util.Scanner;
public class PyramidShape {
     public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the Number of rows : ");
        int rows = in.nextInt();
        int count = 1;
        for (int i = rows; i > 0; i--){
            for (int j = 1; j <= i; j++){
                System.out.print(" ");
            }
            for (int j = 1; j <= count; j++){
                System.out.print(count+" ");
            }
            System.out.println();
            count++;
        }
    }
}

Print the asterisk in diamond shape using java

This is the one of interview question, I've been faced.

Need to print the asterisks in diamond shape. That's a rule.

         *
       *  *
     *  *  *
  *  *   *   *
    *   *   *
       *  *
         *

Like above.

Solution:

import java.util.Scanner;

public class PrintAsterisks {

    public static void main(String[] args) {
        System.out.print("Enter the number of rows : ");
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        for (int i = 1; i <= num; i++) {
            for (int j = num; j >= i; j--) {
                System.out.print(" ");
            }
            for (int m = 1; m <= i; m++) {
                System.out.print(" *");
            }
            System.out.print("\n");
        }
        for (int i = 1; i <= num; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(" ");
            }
            for (int m = num-1; m >= i; m--) {
                …

Sum of the number until it's become single digit in java

Now It's a 50th post. Here after it's should be increase, I hope.

Requirement:
If you're entered number is 123456

1+2+3+4+5+6 = 21 (Two Digit)

2+1 = 3 (Single Digit)

So, we need the single digit.

Here is the solution,

public class DigitSum {

    public static int calculateSum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }

    public static int result(int n) {
        while (n > 9) {
            n = calculateSum(n);
        }
        return n;
    }

    public static void main(String[] arg) {
        System.out.println(result(123456));
    }
}

Swap two variables without using a temporary or additional values

This is normal programming practice.

Solution:
public class SwapVariables{
public static void main(String[] args) {
int a = 2,b = 4;
        System.out.println("Before Swap ");
        System.out.println("Value of A: "+a);
        System.out.println("Value of B: "+b);
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println("After Swap ");
        System.out.println("Value of A: "+a);
        System.out.println("Value of B: "+b);
}
}

Sort the numbers using If, Else and Loop.

This is very interesting. You need to sort the numbers, but not using the SORT functions. You can use the If, Else or Loop to do the functions.

See the solution:

import java.util.Scanner;
public class OrderNumbers {
    public static void main(String[] args) {         Scanner in = new Scanner(System.in);         System.out.println("Enter the size of an Array");         int num = in.nextInt();         int [] numbers = new int[num];         for (int i = 0; i < numbers.length; i++) {             System.out.println("Enter the number "+(i+1)+" : ");             numbers[i] = in.nextInt();         }         sortNumbers(numbers);     }     static void sortNumbers(int[] numbers){         for (int i = 0; i < numbers.length; i++) {             int newValue;             for (int j = 0; j < numbers.length; j++) {                 if (numbers[i] < numbers[j]) {                     newValue = numbers[j];                     numbers[j] = numbers[i];                     numbers[i]…

convert an integer to a string in java

Hi, have a great day. Hope It's like to be homework question or to be interview question. Yes, it's right. In future posts, I'm gonna be post a interview questions and answers, algorithms and some critical problems.

Requirement:
1) return an empty string if value is negative.
2) return an empty string if the conversion fails.
3) return the proper string of the value using characer '0' to '9' and 'A' to 'F'
for example:
MyIntToString(254, 16) return "FE"
MyIntToString(254,  8) return "376"
MyIntToString(254,  2) return "11111110"


Rules:
1) You SHALL NOT import any Java libraries for the implementation.
2) You are allowed to using the string class related functions, like: insert(),
appened, trim()and etc for the implementation
3) You SHALL NOT invoke static functions from java.lang.Integer class to perform the conversion.
4) Your code style is graded, hence it is encouraged to structure the code professionally
and equip…

Show the updated Wi-Fi and Bluetooth status in Android

Hi, Everyone Wish you happy new year 2017. Here we going to see about the show the updated Wi-Fi and Bluetooth status using broadcast receiver.

Add the permissions below in AndroidManifest.xml
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />

In your Activity, Add this,

/* Wifi Status*/
private BroadcastReceiver myWifiReceiver
            = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            NetworkInfo networkInfo = (NetworkInfo) arg1.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                DisplayWifiState();
            }
        }
    };

/*Bluetooth Status*/
private final Broad…