package sort;
public class Test21 {
static int[] s = { 1, 3, 10, 5, 7, 9, 2, 4, 6, 8, 10 };
public static void main(String[] args) {
resot(s);
for (int i : s) {
System.out.println(i);
}
}
public static int[] resot(int[] s) {//其实就是快排单次循环
int low = 0;
int high = s.length - 1;
while (low < high) { //快排使用while单次循环
while (low < high && s[high] % 2 == 0) {//从后往前找奇数
high--;
}
while (low < high && s[low] % 2 == 1) {//从前往后找偶数
low++;
}
if (low < high) { //如果没有到达边界则交换奇数和偶数,继续while
int temp = s[low];
s[low] = s[high];
s[high] = temp;
}
}
return s;
}
}
本文介绍了一个简单的Java程序,用于对数组进行奇偶排序。通过单次循环,将数组中的所有奇数移到前面,偶数移到后面,实现了快速的奇偶分类。代码中详细展示了排序过程,包括寻找奇数和偶数的位置并进行交换。
1152

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



