Array ordering in Java. Knowledge points

Source: Internet
Author: User

Import java.util.*;//Select Sort class switchtest{public static void Main (string[] args) {int[] arr = {3,5,6,23,45,                 2};                 Sort before PrintArray (arr);        Sorted after//Selectsort (arr);        Arrays.sort (arr);//java already defined in a sort order, in development, an array of sorts, to use the sentence code//Bubblesort (arr);    PrintArray (arr);    }/* finds that the element that satisfies the condition needs to be position-displaced regardless of the sort.        So this part of the same code can be extracted, individually encapsulated into a function */public static void Swap (int[] Arr,int a,int b) {int temp = Arr[a];        Arr[a] = arr[b];    ARR[B] = temp;            }//bubble sort public static void Bubblesort (int[] arr) {for (int x=0;x<arr.length-1;x++) {                for (int y=0;y<arr.length-x-1;y++)//-x: Reduces the element for each comparison, 1, avoiding bounds {if (arr[y]>arr[y+1])                    {int temp = Arr[y];                    Arr[y] = arr[y+1];                ARR[Y+1] = temp; }}}}//select sort public static void selectsORT (int[] arr) {for (int. x=0;x<arr.length-1;x++) {for (int y=x+1;y<arr.length;y++)                    {if (Arr[x]>arr[y]) {int temp = arr[x];                    ARR[X] = Arr[y];                Arr[y] = temp;        }}}} public static void PrintArray (int[] arr) {System.out.print ("[");            for (int x=0;x<arr.length;x++) {if (x!=arr.length-1) System.out.print (arr[x]+ ",");        else System.out.println (arr[x]+ "]"); }    }}

Sorting arrays in Java. Knowledge points

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.