Basic algorithm-bubble sort and binary lookup (Java implementation)

Source: Internet
Author: User
Tags array length

Base algorithm: Bubble sort

Bubble sort: Order the unordered data,
The next two elements are compared so that the maximum or minimum values step up, so called bubble sort.

Bubble Sort Idea:
Take ascending order as an example:
In an array, the adjacent two elements a and B are compared, if a is greater than B, A and B swap positions, if less than b do not operate, variable exchange through the intermediate variables.

code example:

public static void main(String[] args) {    int[] a = {3, 1, 6, 5, 2, 9, 7, 8};    int temp = 0;    for(int i=a.length-1;i>0;i--){        for(int j=0;j<i;j++){            if(a[j]>a[j+1]){                temp = a[j+1];                a[j+1]= a[j];                a[j] = temp;            }        }    }打印结果为:12356789

Add ideas for easy comprehension:

i= array Length-1 is the way to cycle through the J+1 method if you do not make the length-1, it will cause the array subscript out of bounds error.

Execution process: When 3>1, move forward, 3 and 6 compared to 6 large, so 6 does not move, 6>5 6 move forward. Because 6>5>3, then 3>1, then 61 fixed >1, at this time 6 is definitely the largest, and 5 is not directly related to the size of the front, so the size of the value can not be determined, need to re-compare, the array for us relatively orderly, but for the bubbling algorithm of the program, There is no difference from disorder.

After the repeat operation, 9 will be in the last one, and our first layer loop has been executed. The second time, another cycle, repeat the operation to the last, only one comparison is the first and second comparison,

Above is the principle and thought of bubble sort.

Two-point Search

When we look for an ordered linear table, and then through the traversal of the full table search, there will be a lot of waste of resources, you can use the idea of binary search to find.

Limitations of using two-point lookups:
1. Linear table
2. Orderly

Two-point search ideas:

Each time you compare the intermediate values of the array, determine the order before and after the comparison, so repeat until you find the desired variable.

Words do not say much directly on the code to parse:

    /** *二分查找 * @param array 传递进来的数组 * @param des 要查找的值 * @return */public static int binarySearch(int[] array,int des){    int low = 0;//设置数组下标最小值    int max = array.length-1;//数组下标最大值    while(low<=max){        int mid = (max+low)>>>1 ;//找到数组中间值.        if(array[mid]==des){            //当中间值正好为想要查找的数值时,直接返回数组下标            return mid;        }else if(array[mid]>des){            //中间值大 所以锁定区域在前面,重新设定范围            max = mid-1;        }else {            //中间值小 所以锁定区域在后面,重新设定范围            low = mid+1;        }    }        //未发现此值时 返回-1        return -1;}

Recursive implementations:

  public static int recursionBinarySearch(int[] array,int des,int low, int max){    int mid = (low+max)>>>1;    if(array[mid]==des){        //当中间值正好为想要查找的数值时,直接返回数组下标        return mid;    }    if(low>=max){        return -1;    }    else if(array[mid]>des){        //中间值大 所以锁定区域在前面,重新设定范围        max = mid-1;        return recursionBinarySearch(array,des,low,max);//将值设定后传入下一层,层层调用 直到相等.    }else if(array[mid]<des) {        //中间值小 所以锁定区域在后面,重新设定范围        low = mid+1;        return recursionBinarySearch(array,des,low,max);//将值设定后传入下一层,层层调用 直到相等.    }    return -1;}

Basic algorithm-bubble sort and binary lookup (Java implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.