在我的小项目中,我按降序对列表进行了排序,但是,我的目标是按照这种自定义模式对其进行排序。(最大 -> 最小 -> 次大 -> 次小 ->)等。
在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 中做同样的事情,除了向后。关于如何将 wackysort 的最后一个 for 循环转换为 python 并使其倒退的任何想法?