This paper illustrates the odd and even sorting algorithm of Java data structure and algorithm. Share to everyone for your reference, specific as follows:
Algorithm idea:
The basic idea is odd column row order, even row order, again odd row, again even row, until all orderly
For example,
arrays to be ranked [6 2 4 1 5 9]
For the first time, the odd sequence is compared with its neighbor pairs, such as 6 and 2, 4 and 1, and 5 and 9.
[6 2 4 1 5 9]
After the exchange becomes
[2 6 1 4 5 9]
Second comparison of even series, i.e. 6 and 1 ratio, 5 and 5 ratio
[2 6 1 4 5 9]
After the exchange becomes
[2 1 6 4 5 9]
The third is the odd sequence, and the 2,6,5 is chosen to compare them to their neighbor columns.
[2 1 6 4 5 9]
After Exchange
[1 2 4 6 5 9]
The four-trip couple series
[1 2 4 6 5 9]
One exchange
[1 2 4 5 6 9]
Specific code:
public class Main {/** * @param args the command line arguments/public STA
tic void Main (string[] args) {int[] numbers = new int[]{12,33,45,33,13,55,34,7,6};
Main.oddeventsort (numbers);
for (int i=0; i<numbers.length; i++) {System.out.print (numbers[i]+ "");
} private static void Oddeventsort (int[] numbers) {int temp; for (int i=numbers.length-1; i>numbers.length/2-1; i--) {for (int j=1; j<=i; j+=2) {if (j==numbers.length
-1) break;
if (numbers[j]>numbers[j+1]) {temp = Numbers[j];
NUMBERS[J] = numbers[j+1];
NUMBERS[J+1] = temp;
(int j=0; j<=i; j+=2) {if (j==numbers.length-1) break;
if (numbers[j]>numbers[j+1]) {temp = Numbers[j];
NUMBERS[J] = numbers[j+1];
NUMBERS[J+1] = temp; }
}
}
}
}
PS: It is said that this algorithm is more useful in the multi-core era
More about Java algorithms Interested readers can view the site topics: "Java data structure and algorithms tutorial", "Java Operation DOM node skills summary", "Java file and directory operation tips Summary" and "Java Cache operation Tips"
I hope this article will help you with Java programming.