Regain Java Series one Java Foundation (3)

Source: Internet
Author: User

This chapter mainly review the previous contact algorithm,

(1) Select the sorting method: In the group of numbers to be sorted, choose the smallest number and the first position of the number of exchanges, and then in the remaining number of the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison.

1 /**2 * Select the sorting algorithm to find the smallest element in the unordered sequence and place it at the beginning of the sort sequence3 * Continue looking for the smallest element from the remaining unsorted elements, and then place it at the end of the sorted sequence that preceded it. 4 * And so on until all elements are sorted. 5      * 6 * @param numbers7      */8      Public Static voidSelectsort (int[] numbers) {9         intsize = Numbers.length;//Array LengthTen         inttemp =0;//Intermediate Variables One  A          for(inti =0; I < size-1; i++) {//I represents the index of the last ranked order -             intK = i;//location to be determined -             //Select the number that should be in the first position the              for(intj = Size-1; J > i; j--) { -                 if(Numbers[j] <Numbers[k]) { -K =j;//as long as the number in the back is smaller than the number of digits to be determined, move forward to the K-corresponding position .18
+ } - } + //Exchange two numbers Atemp =Numbers[i]; atNumbers[i] =Numbers[k]; -NUMBERS[K] =temp; - } -}

(2) Bubble Sort method: It repeatedly visits the sequence to sort, compares two elements at a time, and swaps them if their order is wrong. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.
There are two forms of heavy bubble sinking and light bubble floating:

Bubble Sink:

 Public Static voidBubblesort (int[] ary) {         for(inti =0; I < ary.length-1; i++) {             for(intj =0; J < Ary.length-i-1; J + +) {                if(Ary[j] > ary[j +1]) {                    intt =Ary[j]; ARY[J]= Ary[j +1]; Ary[j+1] =T; }            }        }    }

Light Bubble float:

 Public Static voidBubblesort (int[] ary) {         for(inti =0; I < ary.length-1; i++) {             for(intj = ary.length-1; J >0; j--) {                if(Ary[j] < ary[j-1]) {                    intt =Ary[j]; ARY[J]= Ary[j-1]; Ary[j-1] =T; }            }        }    }

(3) Insert sort: Each step inserts a record to be sorted by its sequential code size into the appropriate position of the previously sorted sequence of words (after finding a suitable position from behind) until all the insertions are sorted out.

1  Public Static voidInsertsort (int[] ary) {2         inti,j,k;3          for(i=1; i<ary.length; i++){4k = Ary[i];//Step1, first taken out as a criterion of judgement5              for(j=i-1; j>=0&& ary[j]>k; j--){6ary[j+1] = Ary[j];//Step2, mobile a[j]-->a[j+1]7             }8ary[j+1] = k;//step3, insertion,9         }Ten}

For other algorithms please refer to: http://www.cnblogs.com/0201zcr/p/4764427.html

Http://www.cnblogs.com/sevenyuan/archive/2009/12/04/1616897.html

Thank you very much, original author

Regain Java Series one Java Foundation (3)

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.