我了解如何array
按升序和降序排序,但我正在尝试创建一个特定的模式。例如,我有array
一个随机顺序。我将如何array
在模式中对其进行排序?“最小的,最大的,第二小的,第二大的,第三小的,第三大的……”等等。有什么想法吗?
int[] pattern = {8, 6, 1, 2, 3, 80, 56};
//这是开始
public class Test2 {
public static void main(String[] args) {
int[] array = {1,4,2,6,9,3,65,77,33,22};
for (int i = 0; i < array.length; i++) {
System.out.print(" " + array[i]);
}
wackySort(array);
}
//This sorts the array
public static void wackySort(int[] nums) {
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;
}
}
}
System.out.println();
int firstPointer = 0;
int secondPointer = nums.length -1;
int[] newarray = new int[nums.length];
for (int i = 0; i < nums.length; i+=2) {
newarray[i] = nums[firstPointer++];
newarray[i] = nums[secondPointer--];
}
for (int i = 0; i < newarray.length; i++) {
System.out.print(" " + newarray[i]);
}
}
}