Java Algorithm Chapter Summary

Source: Internet
Author: User

  1. Bubble sort

    Compare adjacent elements, if the first one is larger than the second, then swap their positions; each pair of adjacent elements is compared sequentially, and the last element should be the largest.

    int[] array = {10,13,12,8,11,6};// Array from small to large sort for  (int i = 0; i < array.length - 1; i++)  {     for (int j = 0; j < array.length - i -  1;j ++) {        if  (array[j] > array[j &NBSP;+&NBSP;1])  {            a =  Array[j];            array[j] = array[j &NBSP;+&NBSP;1];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ARRAY[J&NBSP;+&NBSP;1]  = a;        }    }}system.out.println ( Arrays.tostring (array));//[6, 8, 10, 11, 12, 13] 
  2. Compare sort

    Take the elements in the array, followed by the elements, if they are greater than the subsequent elements, then the exchange of their position, in this way the array after comparison is a small to large sorted array.

    Array from small to large sort for (int i = 0; i < Array.Length; i++) {for (int j = i + 1; j < Array.length;j + +) {if (Array[i]            > Array[j]) {a = Array[i];            Array[i] = Array[j];        ARRAY[J] = A; }}}system.out.println (arrays.tostring (array));//[6, 8, 10, 11, 12, 13]
  3. Recursive

    The way the function itself calls itself is called recursion.

    Public int[] sortarray (int[] array,int index) {    int a =  0;    if (index <= array.length - 1) {         for (int i = index + 1; i < array.length;i  ++) {            if (array[index] >  array[i]) {                 a = array[index];                 array[index] = array[i];                 array[i] = a;             }        }         sortarray (Array,index + 1);     }    return array;} System.out.println (Sortarray (array,0))
  4. Two-point Search

    Two-point lookup speed, data sources need to arrange in order

    Query 11 in the array subscript int queryparam = 11;  Arrays.sort (array);//Sort int i = 0;int start = 0;int end = Array.length-1;while (start <= end) {i = (start + end)/    2;        if (Queryparam = = Array[i]) {System.out.println (i);    Break    } else if (Queryparam < array[i]) {end = i;    } else if (Queryparam > Array[i]) {start = i; }}


Java Algorithm Chapter Summary

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.