Sort of array in Java, direct sort, bubble sort, insert sort

Source: Internet
Author: User

1. Direct Ordering:

 public  static  void  selectsort (int  [] arr) { 
   
    for  (
    int  x = 0; x < arr.length-1; X++
     for  (int  y = x + 1; y < arr.) Length Y++ if  (Arr[x] > Arr[y]) { int  temp = Arr[x]                    ;                    ARR[X]  = Arr[y];                Arr[y]  = temp; }            }        }    }
   

Description: Double for loop, the first number of the array a[0] and all subsequent numbers are compared, get the smallest number, and then the second number a[1] and all the subsequent numbers are compared to get the second small number ... The results are sorted after repeated.

2. Bubble sort

(1),

1  Public Static voidBubblesort (int[] arr) {2 3             for(intx=0; x<arr.length-1; X + +) {4 5                 for(inty=0; y<arr.length-x-1; y++) {6 7                   if(arr[y]>arr[y+1]){8 9                       inttemp =Arr[y];Ten  OneArr[y] = arr[y+1]; A  -ARR[Y+1] =temp; -  the } -  - } -  + } -  +}

(2),

1  Public Static voidSortint[] a) {2       inttemp = 0;3        for(inti = a.length-1; i > 0; --i) {4          for(intj = 0; J < I; ++j) {5           if(A[j + 1] <A[j]) {6temp =A[j];7A[J] = a[j + 1];8A[j + 1] =temp;9 }Ten } One } A}

Description: The bubbling sort is a comparison between 22, such as: 4.2.5.1.7: The first internal loop, 4 and 2 than-->2,4, 4 and 5 than-->4,5, 5 and 1 than-->5,1, 5, and 7 than-->5,7.

So the largest of the number to the end of the array, the second time to make a comparison as long as the array length-2 times, is <arr.length-1, you can get the second largest number, placed in the penultimate position, so repeatedly get sorted array.

3. Insert Sort:

 public  static  void  insertsort ([] a) { for  (int  i = 1; i < a.length; I++ for  (int  j = i; j > 0; J-- if  (A[j] < A[j-1 int  temp = A[j-1-1] = A[j];                A[J]  = temp;            }else break; }        }    }

Description: Insert sort and play cards, get a number and the previous number to compare, than the previous number is placed in front, for example: 4,2,5,1,7:

The first cycle, get a number of a[1]=2, and a[0]=4 contrast, 2 small, change position;

The second cycle, get the number 5, and 4 ratio, the position is unchanged, if the judgment is false, then break, jump out of the inner loop, the next time;

The third cycle, get the number 1, and 5 ratio, exchange position, 1 in and 4 ratio, swap position, finally and 2 ratio, exchange position again, get results, 1,2,4,5,7;

...

Sort of array in Java, direct sort, bubble sort, insert sort

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.