public class Demo {
public static int[] getsz(int length,int max) {
int[] sz = new int[length];
for (int i = 0; i < sz.length; i++) {
sz[i] = (int)(Math.random()*max) +1;
}
return sz;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] aa = getsz(10, 20);
int[] aa2 = new int[10];
System.arraycopy(aa, 0, aa2, 0, 10);
for (int i = 0; i < aa.length; i++) {
System.out.print(aa[i]+" ");
}
System.out.println();
int[] qq = me1(aa);
for (int i = 0; i < qq.length; i++) {
System.out.print(qq[i]+" ");
}
System.out.println();
int[] qq1 = me2(aa2);
for (int i = 0; i < qq1.length; i++) {
System.out.print(qq1[i]+" ");
}
}
public static int[] me1(int[] get) {//利用快排的思想
// TODO Auto-generated method stub
int begin = 0,end = get.length-1;//写出指针
int temp = 0;//辅助值
while (begin<end) {
while (get[end]%2==0) {
end--;
}
while (get[begin]%2==1) {
begin++;
}
if (begin<end) {
temp = get[begin];
get[begin] = get[end];
get[end] = temp;
}
}
return get;
}
public static int[] me2(int[] get) {//利用归并排序的思想 借用辅助空间
int[] help = new int[get.length];
System.arraycopy(get, 0, help, 0, get.length);
//对help有一个指针进行一次扫描
int help_i = 0;
//对原来的数组需要两个指针去控制
int begin = 0,end = get.length-1;
for (int i = 0; i < help.length; i++) {
if (help[i]%2==1) {
get[begin] = help[i];
begin++;
}else {
get[end] = help[i];
end--;
}
}
return get;
}
}
09-11
320