3 Simple sort of Java

Source: Internet
Author: User

1. Bubble sort

public class maxmin{public              static void Main (String args[]) {          int a[]={5,4,9,8,7,6,0,1,3,2};          Bubblesort (a);             Static methods are called by the static method directly through the class name, or through the object to invoke the non-static method for          (int i=0;i<a.length;i++) {             System.out.print (a[i]+ "");}       }            public static void Bubblesort (int array[]) {             int i,j,tmp;           for (i=1;i<array.length-1;i++) {for               (j=array.length-1;j>=i;j--) {                   if (Array[j]<array[j-1]) {                      Tmp=array[j];                      ARRAY[J]=ARRAY[J-1];                      array[j-1]=tmp;}}}}    

2. Insert Sort

public class testsort{public  static void Main (String args[]) {     int array[] = {7,3,19,40,4,7,1};     Insertsort (array);      for (int i=0;i<array.length;i++) {   System.out.print (array[i]+ "");     }  }  public static void Insertsort (Int. a[]) {     if (a!=null) {for        (int i=1;i<a.length;i++) {   int tmp = a[i],j=i;< C11/>while (j>=1&&a[j-1]>tmp) {a[j] = a[j-1];        j--;   }   a[j]=tmp; Each a[i] is the TMP value, as long as it is small, j--, always escort her to the smallest position}}}}  

3. Select sort

the first loop of the Select sorting method starts from the starting element and selects the second-to-last element, mainly by assigning the subscript of the outer loop to the temporary variable before each incoming second loop, and, in the next second loop, if there is a smaller element than the element at the smallest position. The subscript of the smaller element is assigned to the temporary variable, and finally, after the two-layer loop exits, if the temporary variable changes, then there is a smaller element than the current outer loop position, which needs to be exchanged for these two elements.

public class testsort{public       static void Main (String args[]) {          int array[] = {7,3,19,40,4,7,1};          Selectsort (array);      for (int i=0;i<array.length;i++) {       System.out.print (array[i]+ "");     }       }public static void Selectsort (int a[]) {    int minindex=0;    int temp=0;    if ((a==null) | | | (a.length==0)) return;    for (int i=0;i<a.length-1;i++) {       minindex=i;    Assigns the subscript of the outer loop to the temporary variable for       (int j=i+1;j<a.length;j++) {           if (A[j]<a[minindex]) {        minindex=j;            }       }       if (minindex!=i) {//After the two-layer loop exits, if the temporary variable changes, then there is a smaller element than the current outer loop position, the two elements need to be exchanged          tmp=a[i];        A[i]=a[minindex];        a[minindex]=temp;}}}}    



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

3 Simple sort of 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.