13-02 Java Array Advanced algorithm, arrays class

Source: Internet
Author: User

Bubble sort

Bubble Sort Principle

Bubble Sort Code:

 Packagecn.itcast_01;/** Array sort of bubble sort: * Adjacent elements 22 comparison, large back, the first time, the maximum appears at the maximum index*/ Public classArraydemo { Public Static voidMain (string[] args) {//define an array        int[] arr = {24, 69, 80, 57, 13 }; System.out.println ("Before sorting:");        PrintArray (arr);        Bubblesort (arr); System.out.println ("After sorting:");    PrintArray (arr); }        //Bubble Sort Code    /*all you need to do is compare the array length-1 times, x < arr.length-1 * Each time the comparison is complete, the next one will reduce the element comparison. The first comparison has 0 elements that are no more than, the second time there are 1 elements not,,, so y < arr.length-1-X * 22 compared, big back put **/     Public Static voidBubblesort (int[] arr) {        for (int x = 0; x < arr.length-1; + +) {for (int y = 0; y < arr.length-1-X; y++) {                    if (Arr[y] > arr[y + 1]) {int temp = Arr[y];                    Arr[y] = arr[y + 1]; Arr[y + 1] = temp; }            }        }    }    //traversal function     Public Static voidPrintArray (int[] arr) {System.out.print ("[");  for(intx = 0; x < arr.length; X + +) {            if(x = = Arr.length-1) {System.out.print (arr[x]); } Else{System.out.print (arr[x]+ ", "); }} System.out.println ("]"); }}

Select sort

Select Sort schematic

Select Sort Code

 Packagecn.itcast_02;/** Sorting of arrays sorted by: * Starting with 0 index, comparing with subsequent elements, small forward, first completed, minimum value appears at the minimum index*/ Public classArraydemo { Public Static voidMain (string[] args) {//define an array        int[] arr = {24, 69, 80, 57, 13 }; System.out.println ("Before sorting:");                PrintArray (arr); //using methods to improveSelectsort (arr); System.out.println ("After sorting:");    PrintArray (arr); }    /** Array Sort **/     Public Static voidSelectsort (int[] arr) {        for (int x=0; x<arr.length-1; x + +) {for (int y=x+1; y<arr.length; y++) {if (Arr[y] <ar                    R[x]) {int temp = arr[x];                     ARR[X] = Arr[y]; Arr[y] = temp; }            }        }    }    //traversal function     Public Static voidPrintArray (int[] arr) {System.out.print ("[");  for(intx = 0; x < arr.length; X + +) {            if(x = = Arr.length-1) {System.out.print (arr[x]); } Else{System.out.print (arr[x]+ ", "); }} System.out.println ("]"); }}

Two-Part search method

Principle of Binary Search method

The code implementation of the dichotomy method:

 Packagecn.itcast_04;/** Find: * 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: * Big left to find * Small right looking for * D: Recalculate Intermediate index * big left to find * max = mid-1; * Small Right to find * min = mid + 1; * E: Back to B*/ Public classArraydemo { Public Static voidMain (string[] args) {//define an array        int[] arr = {11,22,33,44,55,66,77}; //Write function Implementation        intindex = GetIndex (arr, 33); System.out.println ("Index:" +index); //What happens if this element does not exist?index = GetIndex (arr, 333); System.out.println ("Index:" +index); }        /** Two explicit: * return value type: int * parameter list: int[] arr,int value*/     Public Static intGetIndex (int[] arr,intvalue) {        //define maximum index, minimum index        intmax = Arr.length-1; intMin = 0; //Calculate the intermediate index        intMid = (max +min)/2; //Compare the value of the intermediate index with the value you want to find         while(Arr[mid]! =value) {            if(arr[mid]>value) {Max= Mid-1; }Else if(arr[mid]<value) {min= Mid + 1; }                        //Add judgment if (Min > Max) {return-1; } Mid= (max +min)/2; }                returnmid; }}

Arrays class

 Packagecn.itcast_05;Importjava.util.Arrays;/** Arrays: A tool class that operates on an array. such as sorting and finding. * 1:public static string toString (int[] a) turns the array into a string * 2:public static void sort (int[] a) sorts the arrays * 3:public static int bin Arysearch (int[] a,int key) Two-point lookup*/ Public classArraysdemo { Public Static voidMain (string[] args) {//define an array        int[] arr = {24, 69, 80, 57, 13 }; //Public static string toString (int[] a) turns the array into a stringSystem.out.println ("Before sorting:" +arrays.tostring (arr)); //Public static void sort (int[] a) sorting an arrayArrays.sort (arr); System.out.println ("After sorting:" +arrays.tostring (arr)); //[ +, +, +--]//public static int BinarySearch (int[] a,int key) Two-point lookupSystem.out.println ("BinarySearch:" + arrays.binarysearch (arr, 57)); System.out.println ("BinarySearch:" + arrays.binarysearch (arr, 577)); }}

13-02 Java Array Advanced algorithm, arrays class

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.