public static void smallestDes(){
if(array==null||array.length%2!=0){
System.out.println("wrong");
return;
}
quick(0, array.length-1); //将原数组从大至小进行排序
int[] arr=turn(); // 因为我自己写的quick函数是从小到大排序,所以写了一个turn函数,将数组转换为从大到小排序
int[] firstArr=new int[arr.length/2]; //假如你不关心分组情况只关心两分组差值的话,可以不需要
int[] secondArr=new int[arr.length/2]; //同上
firstArr[0]=arr[0];
int firstSum=arr[0];
int firstTotalEleNum=1;
int secondSum=0;
int secondToalEleNum=0;
for(int i=1;i<arr.length;i++){
// System.out.println(i);
// System.out.println(secondToalEleNum);
// System.out.println();
if(firstSum>secondSum&&secondToalEleNum<secondArr.length){
secondArr[secondToalEleNum++]=arr[i];
secondSum+=arr[i];
}
else{
firstArr[firstTotalEleNum++]=arr[i];
firstSum+=arr[i];
}
// if(firstTotalEleNum==){
// for(int j=i;j<arr.length;j++){
// secondArr[secondToalEleNum++]=arr[j];
// secondSum+=arr[j];
// }
// over=true;
// }
// if((secondToalEleNum-firstTotalEleNum)==arr.length-i){
// for(int j=i;j<arr.length;j++){
// firstArr[firstTotalEleNum++]=arr[j];
// firstSum+=arr[j];
// }
// over=true;
// }
}
System.out.println("Smallest Sub:"+"firstSum:"+firstSum+" secondSum:"+secondSum+" result:"+(firstSum-secondSum));
}
}
试探性解决,如有错误希望共同探讨