There are several common ways to sort arrays in Java!

Source: Internet
Author: User

Definition of the array:

     int New int [5];         int [] arr1 = {1,2,3,4,5};                 Long New Long [6];                 New String[5];                 New PERSON[5];

Operation of the array:

int [] arr = {A,        a, a. Arrays.sort (arr);        System.out.println (arrays.tostring (arr));                 // binary search method (need to sort before use)        int i = Arrays.binarysearch (arr.);        System.out.println (i);         int [] NewArr = arrays.copyof (arr, 7);         int [] newArr1 = Arrays.copyofrange (arr, 1, 3);        System.out.println (arrays.tostring (NEWARR));        System.out.println (arrays.tostring (NEWARR1));                 int j = Arrays.binarysearch (arr, 1, 3,);        System.out.println (j);

  Bubble Sort:

Int[] arr = {23,12,48,56,45};
     int
temp =-1; for (int i=0;i<arr.length;i++) { for (int j=i+1;j<arr.length;j++) { if (arr[i]>arr[j]) { = arr[i]; = Arr[j]; = temp;}} } System.out.println (arrays.tostring (arr));

  Direct Selection Sort:

Int[] arr = {23,12,48,56,45};
 for (int i=0;i<arr.length;i++) {            int tem = i;              for (int j=i;j<arr.length;j++) {                if(Arr[j] < Arr[tem]) {                    =  J;                }            }             int Temp1 = arr[i];             = Arr[tem];             = temp1;        }        System.out.println (arrays.tostring (arr));

  Reverse Sort:  

Int[] arr = {23,12,48,56,45};
 for (int i=0;i<arr.length/2;i++) {            int temp = arr[i];             = Arr[arr.length-i-1];            Arr[arr.length-i-1] = temp;        }        System.out.println (arrays.tostring (arr))

There are several common ways to sort arrays in Java!

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.