Java Basic Learning Notes _ four sorting methods

Source: Internet
Author: User
Tags rounds

Default Small to large sort

① Quick Method Sorting

First round: The first number in turn compared with the following number, if the first number is larger, then two number interchange position, that is, the smallest number is placed in the first place

 for (i=0;i<a.length-1;i++)       // control comparison rounds     for (j=i+1;j<a.length;j++)   // is always compared to the number in the back.        if (a[i]>a[j])        {            t=a[i];            A[i]=A[j];            A[J]=t;                   }

② Selection Method Ordering

First round: 1-n number, find the minimum number, and then swap the position with the first number

 for (i=0;i<a.length-1;i++)        // Control rounds {    k=i;                         // assume the minimum number     for (j=i+1;j<a.length;j++)        if (A[k]>a[j]) k=j;        // Find the minimum number    of        current rounds if (i!=k)        {            t=a[i];            A[i]=a[k];            A[k]=t;                   }}

③ Bubble Method Sort

First round: The first and second numbers are compared, the big one is followed, and then the second number (larger) is compared to the third number, so that the largest number floats to the top (the last digit position)

 for (i=0;i<a.length-2;i++)       // control comparison rounds     for (j=0;j<a.length-1;j++)   // Control comparison Range        if (A[j]>a[j+1])        {            t=a[j];            A[J]=a[j+1];            A[j+1]=t;                   }


④ Insertion Method Sorting

First round: The second number is compared with the first number, if the second number is larger then the first number is placed, otherwise placed in the first number of positions, the first number forward a

 for  (i=1;i<a.length;i++) //  Put the remaining n-1 number  { for  (j=0;j<i;j++) //  and then the number of ordered comparisons, from small to large 
     if  (A[i]<a[j]) break ; //               x=a[i]; //     for      (T=i-1;t>=j;t-- +1]=a[t]; //               a[j]=x in the determined position and subsequent numbers; //  The current number is placed in the OK position } 


The Arrays.sort method can be easily solved if it is just a one-dimensional array ordering.

Java Basic Learning Notes _ four sorting methods

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.