Java common 4 sorts of sorting methods __java

Source: Internet
Author: User
Tags constant sorts

Java common 4 sorts of sorting methods

In Java, when using array to sort function, there are generally four kinds of methods: fast sorting method, bubbling method, choosing sorting method, inserting Sort method.

The fast sorting method mainly utilizes a method of arrays to Arrays.sort () implementation.

Bubble method is used to compare the traversal array, through the constant comparison of the minimum or maximum value of a single traversal.

The Select Sort method is to use the first data of the array as the largest or smallest value, and then output the ordered array by comparing the loops.

The insertion sort is the selection of data in an array, which is sorted by constant insertion comparisons. Below I will be their implementation method one by one detailed explanation for everybody's reference.

<1> using arrays with sorting method to quickly sort

Copy Code

1 Import java.util.Arrays;

2 public class test2{

3 public static void main (string[] args) {

4 int[] a={5,4,2,4,9,1};

5 Arrays.sort (a); To sort

6 for (int i:a) {

7 System.out.print (i);

8}

3 ·

10}

Copy Code

<2> Bubble Sort algorithm

Copy Code

1 public static int[] Bubblesort (int[] args) {//bubble sort algorithm

2 for (int i=0;i<args.length-1;i++) {

3 for (int j=i+1;j<args.length;j++) {

4 if (Args[i]>args[j]) {

5 int temp=args[i];

6 args[i]=args[j];

7 args[j]=temp;

8}

3 ·

10}

one return args;

12}

Copy Code

<3> Selection Sorting algorithm

Copy Code

1 public static int[] Selectsort (int[] args) {//select sorting algorithm

2 for (int i=0;i<args.length-1; i++) {

3 int min=i;

4 for (int j=i+1;j<args.length; j + +) {

5 if (Args[min]>args[j]) {

6 min=j;

7}

8}

9 if (min!=i) {

ten int temp=args[i];

One args[i]=args[min];

Args[min]=temp;

13}

14}

return args;

16}

Copy Code

<4> Insert Sorting algorithm

Copy Code

1 public static int[] Insertsort (int[] args) {//Insert sort algorithm

2 for (int i=1;i<args.length;i++) {

3 for (int j=i;j>0;j--) {

4 if (Args[j]<args[j-1]) {

5 int temp=args[j-1];

6 args[j-1]=args[j];

7 args[j]=temp;

8}else break;

9}

10}

one return args;

12}

Copy Code

These are the four sorting methods in Java. Different methods of efficiency are not the same, the following is the comparison of different algorithms and data exchange when the large O representation.


Bubble sort: compare O (N2) data exchange O (N2)

Select sort: Compare O (N2) data exchange O (N)

Insert Sort: compare O (N2) copy data O (N)

In the practical application, we should try to choose the algorithm with high efficiency.

For learning difficulties do not know how to enhance their own can be added to the deduction: 578024144 Exchange to get help, access to learning materials

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.