Four kinds of sorting methods in Java using arrays

Source: Internet
Author: User

<1> quick sorting using arrays with sorting methods

<span style= "Font-family:simsun;font-size:18px;color: #000099;" >import java.util.Arrays; 2 public class test2{public           static void Main (string[] args) {                   int[] a={5,4,2,4,9,1};                   Arrays.sort (a);  Sort for                   (int i:a) {                           System.out.print (i);}                  }  } </span>
<2> bubble Sorting algorithm

public static int[] Bubblesort (int[] args) {//bubble sort algorithm for           (int. i=0;i<args.length-1;i++) {for                  (int j=i+1;j< args.length;j++) {                      if (Args[i]>args[j]) {                          int temp=args[i];                          ARGS[I]=ARGS[J];                          Args[j]=temp         ;         }}} return args;  

<3> Selection Sorting algorithm

<span style= "Font-family:simsun;font-size:18px;color: #000099;" >public static void Selectsort (Int[]a) {    int minindex=0;    int temp=0;    if ((a==null) | | | (a.length==0))        return;    The        minimum Data array for the for (int i=0;i<a.length-1;i++) {minindex=i;//unordered region subscript for        (intj=i+1;j<a.length;j++)        {            //Find the smallest data in the unordered area and save its array subscript            if (A[j]<a[minindex])            {                minindex=j;            }        }        if (minindex!=i)        {            //if the minimum position of the unordered area is not the default first data, then the interchange.            Temp=a[i];            A[i]=a[minindex];            a[minindex]=temp;}}}    </span>
<span style= "Font-family:simsun;font-size:24px;color: #3333ff;" ><strong><4> Insert Sort Algorithm </strong></span>
<span style= "Font-size:18px;color: #6600cc;" >public Static int[] Insertsort (int[] args) {if (args==null| | ARGS.LENGTH<2) {return args;}       for (int i=1;i<args.length;i++)   {for                (int j=i;j>0;j--) {                      if (args[j]<args[j-1])   {                              int TEMP=ARGS[J-1];                             ARGS[J-1]=ARGS[J];                             args[j]=temp;                                 } else break;                    }             }      return args;  }  </span>




Four kinds of sorting methods in Java using arrays

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.