Java:数组排序(第2部分)
我知道如何按升序和降序对数组进行排序,但我正在尝试创建一个特定的模式。例如,我有一个随机排列的数组。如何在模式中对该数组进行排序?“最小的,最大的,第二小的,第二大的,第三小的,第三大的……”等等,有什么想法吗Java:数组排序(第2部分),java,Java,我知道如何按升序和降序对数组进行排序,但我正在尝试创建一个特定的模式。例如,我有一个随机排列的数组。如何在模式中对该数组进行排序?“最小的,最大的,第二小的,第二大的,第三小的,第三大的……”等等,有什么想法吗 public class Test2 { /** * @param args the command line arguments */ public static void main(String[] args) { int[] array = {1,4,2,6,9,3,
public class Test2 {
/**
* @param args the command line arguments
*/
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);
}
public static void wackySort(int[] nums){
int sign = 0;
int temp = 0;
int temp2 = 0;
//This sorts the array
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;
}
}
}
// This prints out the new array
System.out.println();
for(int i = 0; i < nums.length; i++){
System.out.print(" " + nums[i]);
}
System.out.println();
//This part attempts to fix the array into the order I want it to
int firstPointer = 0;
int secondPointer = nums.length -1;
int[] newarray = new int[nums.length];
for (int i = 0; i < nums.length -1; i+=2){
newarray[i] = nums[firstPointer++];
newarray[i] = nums[secondPointer--];
}
for(int i = 0; i < newarray.length; i++){
System.out.print(" " + newarray[i]);
}
}
}
公共类Test2{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
int[]数组={1,4,2,6,9,3,65,77,33,22};
for(int i=0;inums[j+1]){
温度=毫微秒[j];
nums[j]=nums[j+1];
nums[j+1]=温度;
}
}
}
//这将打印出新阵列
System.out.println();
对于(int i=0;i
您需要编写自己的代码来实现这一点。研究这篇文章。这将对您有所帮助。您不应该在此处发布相同的问题。查看上次postOk的更新答案,谢谢。如何删除此帖子?