A Collection of Common Algorithms in android <1>

Keywords: Android

In android, some algorithms are often used to implement some complex logic.
Five algorithms are introduced in this blog:

    1. Selection sort;
    2. Insertion sort;
    3. Quick sorting;
    4. Merge sort;
    5. heap sort;

Before looking at the following, we need to pay attention to: some algorithmic language expression is relatively weak, when looking at the following blog, we should pay attention to the following code on top of the code can be used for personal testing;

1. Selection sort

Select an element in an array from one end of the array and place the minimum (or maximum) on the left side of the array by comparing and contrasting (the right side mainly depends on whether the beginning of the traversal is left or right); traverse one by one;
Look at the code below:

 /**Selection sort
 *Traverse the array and then continue to traverse all the elements after each element and find the smallest element and its replacement location.*/
    public static int[] select(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minPos = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minPos]){
                    minPos = j;
                }
            }


            if (array[i] > array[minPos]){
                int temp = array[i];
                array[i] = array[minPos];
                array[minPos] = temp;
            }

        }
        return array;

    }

2. Insertion sort

In the same way, the array is traversed one by one, and the traversed elements and their preceding elements are arranged by two comparisons at a time. When the traversal is completed, the sorting is also completed.

    /***Insertion sort
     *It's equivalent to starting at 0 and then going all the way to + + and then arranging the index to follow the previous elements.
     * */
    public static int[] insert(int[] array){
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            for (int j = i - 1; j >= 0 ; j--) {
                if (array[j] > temp ){
                    array[j + 1] = array[j];
                    array[j] = temp;
                }

            }

        }
        return array;
    }

3. Quick Sorting

Let's look at quick sorting. Select an element (usually the first element of the array) and then as the target element, place the smaller element in the array on the left of the target element and the larger one on the right of the target element.
Then continue to arrange the left and right array s in the same way; let's look at the code below:

/**Quick sort
     * To set a watershed in a fast row is to randomize an element and then place it smaller than him to the left and larger than him in a circular circle.
     * */
    public static int[] quick(int[] array){

        int length = array.length - 1;

        sortQuick(array,0,length);



        return array;
    }

    private static void sortQuick(int[] array, int low, int length) {
        while (low < length){
            int div = firstRun(array, low, length);
            firstRun(array,0,div - 1);
            firstRun(array,div + 1,length);
        }
    }

    private static int firstRun(int[] array, int low, int length) {
        int base = array[low];

        while (low < length){
            while (array[length] > base){
                length--;
            }
            swrap(array,low,length);
            while (array[low] < base){
                low++;
            }
            swrap(array,low,length);
        }
        return low;
    }

    private static void swrap(int[] array, int low, int length) {
        int temp = array[length];
        array[length] = array[low];
        array[low] = temp;
        //return array;
    }

4. Merge Sort

Merge sorting is to split arrays, and then more of them are sorted by insertion sorting, which is used to deal with elements or more complex situations, divide and conquer them, and merge them into one.

 /**Merge sort
     * Always dividing
     * Then the two parts are split and then recursive*/
    public static int[] mesh(int[] array){
        int length = array.length - 1 ;
        int[] mysort = mysort(array, 0, length);
        return mysort;

    }

    private static int[] mysort(int[] array, int low, int length) {
        int mid = (length + low)/2;
        if (low < length){
            mysort(array,low,mid);
            mysort(array,mid + 1,length);
            merge(array,low,mid,length);
        }
        return array;

    }

    private static void merge(int[] array, int low, int mid, int length) {
        int[] temp = new int[length - low + 1];

        int i = low;
        int j = mid + 1;
        int n = 0;
        while (i <= mid && j <= length){
            if (array[i] > array[j]){
                temp[n++] = array[j++];
            }else {
                temp[n++] = array[i++];
            }
        }

        while (i <= mid){
            temp[n++] = array[i++];
        }


        while (j <= length){
            temp[n++] = array[j++];
        }


        //Merged logical assignment to array

        for (int k = 0; k < temp.length; k++) {
            array[low + k] = temp[k];
        }


    }

5. heap sort

Let's start with heap: heap is a complete binary tree with any root element larger (or smaller) than or equal to a child.
In fact, heap sorting is a process of building a large or small top heap all the time, then changing the top and the last elements of the heap, removing the top, reconstructing other elements into the top heap, replacing the top elements again, replacing them again, and so on.
ok~Look at the code below:

/**
     * Heap algorithm large-top heap and small-top heap
     * Every time the stack is fetched and set, then put into the array reconfiguration heap, and then fetch and reconstruct.
     * */
    public static int[] heap(int[] array){
        int length = array.length - 1;
        for (int i = 0; i < length; i++) {
            buildHeap(array,length - i);
            swapHeap(array,0,length - i);
        }


        return array;
    }

    private static void swapHeap(int[] array, int a, int b) {
        int temp = array[b];
        array[b] = array[a];
        array[a] = temp;

    }

    private static void buildHeap(int[] array, int lastIndex) {
        //Access to left and right children
        int pos = (lastIndex - 1)/2;
        for (int i = pos; i >= 0; i--) {
            int childL = i * 2 + 1;
            int childR = i * 2 + 2;
            int maxIndex = childL;
            if (childR <= lastIndex){
                //Right child
                if (array[childL] < array[childR]){
                    maxIndex ++;
                }
            }else if (childL <= lastIndex){
                //Left child
            }

            if (array[i] < array[maxIndex]){
                swrap(array,i,maxIndex);
            }
        }


    }

Thank you for reading and looking forward to your comments.

Posted by gtrufitt on Tue, 21 May 2019 17:51:05 -0700