Several common sorting algorithms of Java Foundation

Source: Internet
Author: User
Tags benchmark

One, bubble sort

1. Principle:

Starting from the first position of the array 22 compare Array[index] and array[index+1], if ARRAY[INDEX] is greater than array[index+1], the position of the interchange Array[index] and array[index+1], Stop to the end of the array;

    • Starting from the first position of the array, repeat the above action, ending with an array length minus one position;
    • Starting from the first position of the array, repeat the above action, ending with an array length minus two positions;
    • .......

2. Time complexity: O (N2), carried out (n-1) * (n-2) .... =n* (n-1)/2 comparisons and the number of exchanges in half (in all cases), then the time complexity is O (n^2) According to the large O notation

3. Code

    int x[]={7,1,2,3,4,5,4,3};     int y;      for (int i = 0; i < x.length; i++) {        for (int j = 0; j < x.length-i-1; j++<
   
     c13>) {            
    if (x[j]>x[j+1
    ]) {                y=x[j+1
    ];                X[j+1]=
    x[j];                X[J]=
    y;            }        }        System.out.println (X[i]);    }
   

Second, choose the sort

1, principle: Select a value array[0] as a benchmark, and then loop to find the smallest value except this value (find the minimum value less than the benchmark), exchange these two values, then the minimum value is placed on the array[0], and then the array[1] as a benchmark, from the remaining unsorted value found in the minimum value, And swap the two values.

2. Time complexity: O (n^2), reducing the number of array exchanges compared to bubble sort

3, Code,

//Select Sort        intX[] ={7,2,2,1,4,8,9,2,4};  for(inti = 0; i < x.length; i++) {            intmin=i;  for(intj = i+1; J < X.length; J + +) {                if(x[j]<X[min]) {min=j;//Put the smallest value first and then click                }            }            if(min! =i) {                inttemp =X[min]; X[min]=X[i]; X[i]=temp; }    }         for(inti = 0; i < x.length; i++) {System.out.println (x[i]); }

Three, insert sort

1. Principle: The idea of inserting a sort is that the array is partially ordered, and then the unordered part loops are inserted into the ordered sequence

    

The first loop is the second element and the first element is compared

The second loop is the third and second comparison, and if it is smaller than the second, then the comparison with the first one

(picked from data structures and algorithms)

2. Time complexity: The time complexity of the sequence in which the order is inserted is also O (n^2), but the order time complexity for the basic ordered sequence is O (N)

3. Code

intX[] ={3,4,5,1,2,3,7,8,9};  for(inti = 1; i < x.length; i++) {                inttemp =X[i]; intJ =i-1;  while(J>=0 && x[j]>temp) {X[j+1]=X[J];//compare in sequence with previousj--; } x[j+1] =temp; }         for(inti = 0; i < x.length; i++) {System.out.println (x[i]); }

Several common sorting algorithms of Java Foundation

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.