前面我们讲解了奇偶排序算法,现在我们用java代码来实现下,直接上代码
package ttt;
public class OddEvenSort {
public static int[] oddEvenSort(int[] theArray) {
boolean oddFlag = false;
boolean evenFlag = false;
while(!oddFlag ||!evenFlag) {
evenFlag = true;
int temp = 0;
for(int i=0 ; i<theArray.length-1; i+=2) {
if (theArray[i] > theArray[i+1]) {
temp = theArray[i];
theArray[i] = theArray[i+1];
theArray[i+1] = temp;
evenFlag = false;
}
}
oddFlag = true;
temp = 0;
for(int j=1 ; j<theArray.length-1; j+=2) {
if (theArray[j] > theArray[j+1]) {
temp = theArray[j];
theArray[j] = theArray[j+1];
theArray[j+1] = temp;
oddFlag = false;
}
}
}
return theArray;
}
public static void main(String[] args) {
int []the

本文通过java代码详细展示了如何实现奇偶排序算法。通过实例演示,读者可以清晰理解该算法的执行过程。
最低0.47元/天 解锁文章
433

被折叠的 条评论
为什么被折叠?



