Day 11Java Basic Learning Notes

Source: Internet
Author: User

Bubble sort adjacent Elements 22 comparison, large back, the first time, the maximum appears at the maximum index, the second is thicker, the maximum value is placed in the penultimate position, until the second element is determined, the order of the entire array is determined
    public class ArrayDemo {        public static void main(String[] args) {            int[] arr = {7,3,6,1,9,4,0};            System.out.println("排序前");            method(arr);            bubbleSort(arr);            System.out.println("排序后");            method(arr);        }        //实现升序排列        public static void bubbleSort(int[] arr){            for (int i = 0; i < arr.length-1; i++) {                for (int j = 0; j < arr.length-1-i; j++) {                    if (arr[j]>arr[j+1]) {                        int temp = arr[j];                        arr[j]= arr[j+1];                        arr[j+1] = temp;                    }                }            }        }        //自定义数组打印方法        public static void method(int[] arr) {            for (int i = 0; i < arr.length; i++) {                System.out.print(arr[i]);            }            System.out.println();        }    }
Select sort

Starting with the 0 index, and then comparing it to the following elements, the small forward, the first time, the minimum appears at the minimum index, and so on, until the second and last number, and so on, the greater is the number of the first position in the countdown is the maximum value in all numbers

    public class ArrayDemo1 {        public static void main(String[] args) {            int[] arr ={3,4,8,1,2,5,7,6};            System.out.println("排序前");            print(arr);            selectSort(arr);            System.out.println("排序后");            print(arr);        }public static void selectSort(int[] arr){            for (int i = 0; i < arr.length-1; i++) {                for (int j = i+1; j < arr.length; j++) {                    if(arr[i]>arr[j]){                        int temp = arr[i];                        arr[i] = arr[j];                        arr[j] = temp;                    }                }            }        }        public static void print(int[] arr){            for (int i = 0; i < arr.length; i++) {                System.out.print(arr[i]);            }            System.out.println();        }    }
The biggest difference between the two sorts is the start and end values of the inner loop.
    • Bubble sort: The inner loop start value starts at 0 and the end value is variable
    • Select Sort: The start value of the inner loop is variable, and the ending value is fixed, which is the maximum index value.
Binary lookup (binary lookup)
    /* Search: * Basic find: Array elements unordered (find tail from scratch) * Binary lookup (binary lookup): array elements ordered * * Analysis: * A: Define maximum index, minimum index * B: Calculate Intermediate Index * C: Compare the value of the intermediate index with the value to find * Equal: Returns the current intermediate index * Unequal: * Large left     Looking for a small right to find * D: Recalculate Intermediate index * big left to find * max = mid-1;     * Small Right to find * min = mid + 1; * E: Back to B * * public class ArrayDemo3 {public static void main (string[] args) {int[] arr = {3            , 4,2,8,5,6,1};            Selectsort (arr);            int res = GetIndex (arr, 6);        System.out.println (RES);  }//define method, first arrange the array public static void Selectsort (int[] ch) {for (int i = 0; i < ch.length-1; i++)                        {for (int j = i+1; J < Ch.length; J + +) {if (Ch[i]>ch[j]) {                        int temp = Ch[i];                        Ch[i] = Ch[j]; CH[J] = temp;                    }}}} public static int getindex (int[] arr, int a) {            Define maximum index, minimum index int max = arr.length-1;            int min = 0;            Calculates the intermediate index int mid = (Min+max)/2;                while (true) {if (arr[mid]==a) {return mid;                    }else{if (arr[mid]<a) {min = Mid +1;                    }else{max = mid-1;                    } if (Min>max) {return-1;                } mid = (Min+max)/2; }            }        }    }
Arrays class overview

The system provides a tool class for manipulating an array

Provides the ability to sort, find, and more. We don't have to write the algorithm manually, just take it.

Static methods
    • public static string ToString (Int[] a): Converts an array to a string
    • public static void sort (int[] a): sort an int array in ascending order ascending
    • public static int BinarySearch (int[] a,int key): Binary lookup in array

      调用方法:Arrays.方法名(形参)如:Arrays.sort(arr);

Day 11Java Basic Learning Notes

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.