Array ordering in Java (Quick sort, bubble sort, select sort) _java

Source: Internet
Author: User

1. Use the sort () in the arrays class in the Javaapi document to sort quickly

Copy Code code as follows:

Import Java.util.Arrays;

public class testone{

public static void Main (String [] args) {

int [] array={2,0,1,4,5,8};

Arrays.sort (array);//Call Arrays static method sort, sorted in ascending order

for (int show:array) {

System.out.println (show);

}

}

}


2, bubble sort
Copy Code code as follows:

public class testtwo{

public static void Mian (String []args) {

int [] arr={2,0,1,4,6,5};

int [] Showarr testMethod (arr);

for (Show:showarr) {

System.out.println (show);

}

}

public static int [] TestMethod (int [] array) {

for (int x=0;x<array.length-1;x++) {

for (int y=x+1;y<array.length;y++) {

if (array [x]>array[y]) {

int temp=array[x];//temporary variable record maximum value

Array[x]=array[y];

Array[y]=temp;

}

}

}

return array;

}

}


3, select the sort
Copy Code code as follows:

public class testtwo{

public static void Mian (String []args) {

int [] arr={2,0,1,4,6,5};

int [] Showarr testMethod1 (arr);

for (Show:showarr) {

System.out.println (show);

}

}

public static int [] TESTMETHOD1 (int [] array) {

for (int x=0;x<array.length-1;x++) {

int min=x;//Set the following table to the minimum value of x

for (int y=x+1;y<array.length;y++) {

if (Array[max]>array[y]) {

Min=j

}

}

if (min!=x) {

int temp=array[x];

Array[x]=array[min];

Array[min]=temp;

}

}

return array;

}

}

Related Article

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.