The examples in this article describe Java four basic sorting methods. Share to everyone for your reference. Specifically as follows:
Java four basic sort, including bubble method, insert method, selection method, Shell sort method. The selection method is an improvement of the bubbling method, and the shell sort method is an improvement of the insertion method. So basically, there are two different sorts of sorting methods: namely: Insert method and Bubble method
One insertion method:
Iterate through the sorted collection, each time to an element, to compare this element with all of its preceding elements, so that elements that conform to the sort order are moved to where it most should appear in the current range. The exchange is the adjacent traversal movement, the double loop control realizes. This sort of sorting method belongs to the Jianglong type, in my land card I want to put all things in a certain order of the regular, come over a, regular one.
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 bubbling method:
Relatively easy, its inner loop ensures that the smallest (large) element in the set appears in its correct position once it is traversed, and the next time it is the second small element ... The number of exchange moves in the various cases of the set distribution is basically the same, which belongs to the slowest sort. Implementation is also a double loop control. This sort of method belongs to the River Dragon, is to find the extreme, but the award Dragon also has eldest brother, two elder brother, so they can only be eldest brother picked two elder brother pick.
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 selection method:
This method only by traversing the collection record smallest (large) element position, once traversing, then carries on the exchange position operation, resembles bubbles, but in the comparison process, does not carry on the exchange operation, only records the element position. One-time traversal is only one swap operation. This is more appropriate for elements that are time-consuming to exchange order. This sort of method is much deeper than the bubbling method, I first remember the extreme data, after traversing the data, I deal with it, unlike the bubble method as long as the extreme point is to deal with, the selection method only to deal with the most extreme data within their own.
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 sort:
It is an improvement to the insertion order, is to consider the collection elements according to a certain base divided into groups to sort, so that each group in the local scope of the first line into the basic order, and finally in the insertion of all the elements of the order.
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 your Java programming.