ここでの私の小さなプロジェクトでは、リストを降順で並べ替えましたが、私の目標は、このカスタム パターンで並べ替えることです。(最大 -> 最小 -> 次に大きい -> 次に小さい ->) など。
Javaでは、次のようにこれを行うことができました:
public static void wackySort(int[] nums) {
//first, this simply sorts the array by ascending order.
int sign = 0;
int temp = 0;
int temp2 = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length -1; j++){
if (nums[j] > nums[j+1]) {
temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
//prepare for new array to actually do the wacky sort.
System.out.println();
int firstPointer = 0;
int secondPointer = nums.length -1;
int[] newarray = new int[nums.length];
int size = nums.length;
//increment by two taking second slot replacing the last (n-1) term
for (int i = 0; i < nums.length -1; i+=2) {
newarray[i] = nums[firstPointer++];
newarray[i+1] = nums[secondPointer--];
}
//store those values back in the nums array
for (int i = 0; i < nums.length; i++) {
nums[i] = newarray[i];
}
}
私の目標は、後方を除いて、同じことをPythonで行うことです。奇抜な並べ替えを行う最後の for ループを python に変換し、逆方向にする方法についてのアイデアはありますか?