Four basic java sorting methods

Source: Internet
Author: User

Java includes four basic sorting methods: Bubble, insert, choice, and shell sorting. the selection method is the improvement of the bubble method and the shell sorting method is the improvement of the insertion method. therefore, it can be summarized as two different sorting methods: insert method and bubble method.

One Insertion Method: traverses the sorting set. Each time an element is retrieved, it must be compared with all its previous element traversal, move the elements that match the sorting order one by one to the most appropriate position in the current range. Exchange is implemented through adjacent traversal and dual-loop control. This sort method belongs to the class of the geographic snake. On my card, I want to sort all the items in a certain order.

The processing code is as follows:

Public void sort (INT [] data ){
Int temp;
For (INT I = 1; I <data. length; I ++ ){
For (Int J = I; (j> 0) & (data [J]> data [J-1]); j --){

Temp = date [J];

Data [J] = data [J-1];

Data [J-1] = temp ;}
}
}

2. Bubble Method: This method is relatively easy. Its inner loop ensures that the smallest (large) element in the set appears in the correct position after the traversal, and the next time it appears as a small element... In various cases of set distribution, the number of switching moves remains unchanged, which is the slowest sort. Implementation is also a dual loop control. This sort method belongs to the pig dragon, that is, to find the extreme, but the award dragon also has the eldest brother and the second brother, so they can only pick the second brother.

The processing code is as follows:

Public static int [] maopao (INT [] data ){
Int temp;
For (INT I = 0; I <data. Length-1; I ++ ){
For (Int J = I + 1; j <data. length; j ++ ){
If (data [I] <data [J]) {
Temp = data [I];
Data [I] = data [J];
Data [J] = temp;
}
}
}

Return data;

Three-choice method: This method only traverses the location of the smallest (large) element of a set record. After one traversal, it performs the switch position operation, similar to bubbling. However, during the comparison process, only the element position is recorded. One traversal only performs one switching operation. This is suitable for elements that are time-consuming compared with the Exchange Order. This sort method is much deeper than the bubble method. I remember the extreme data first. After the data is traversed, I will process it again, unlike the bubble method, the method only processes the most extreme data in its own range.

Public static void xuanze (INT [] data ){
Int temp;
For (INT I = 0; I <data. length; I ++ ){
Int lowindex = I;
For (Int J = data. Length-1; j> I; j --){
If (data [J]> data [lowindex]) {
Lowindex = J;
}
}
Temp = data [I];
Data [I] = data [lowindex];
Data [lowindex] = temp;
}
}

Four shell sorting: it is an improvement of insert sorting. It is to sort the set elements in groups according to a certain base number, each group is arranged in a basic order in a local range, and all elements are inserted and sorted at a time. Public void sort (INT [] data ){
For (INT I = data. Length/2; I> 2; I/= 2 ){
For (Int J = 0; j <I; j ++ ){
Insertsort (data, J, I );
}
}
Insertsort (data, 0, 1 );
}

Private void insertsort (INT [] data, int start, int Inc ){
Int temp;
For (INT I = start + Inc; I <data. length; I + = Inc ){
For (Int J = I; (j> = Inc) & (data [J] <data [J-INC]); j-= Inc) {temp = data [J]; data [J] = data [J-INC] DATA [J-INC] = temp;
}
}
} The above are the basic sorting of the four types of data. After dinner, sort the data in a regular order.

 

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.