Examples of four basic sorting methods in JAVA and four sort instances in java

Source: Internet
Author: User

Examples of four basic sorting methods in JAVA and four sort instances in java

This article describes four basic sorting methods in JAVA. Share it with you for your reference. The details are as follows:

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:

Traverse the sorting set. Each time an element is retrieved, it must be compared with all previous elements, 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; }} }

Two bubble method:

It is relatively easy. Its inner loop ensures that, after one traversal, the smallest (large) element in the set appears in its correct position, and the next time it is 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 in the set record. After one traversal, it performs the switch position operation, similar to the bubble operation. However, during the comparison process, no switch operation is performed, only records the element position. 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 into groups based on a certain base, so that each group is arranged in a basic order in a local range, finally, sort all elements 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; }}}

I hope this article will help you with java programming.

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.