3

我了解如何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]);
        }
    }
}
4

5 回答 5

5

好像是功课。这么简单的提示

步骤 1:按升序对数组进行排序

{1, 2, 3, 6, 8, 56, 80};

步骤 2:分配一个相同大小的新数组

第 3 步遍历第一个数组,两个计数器。一开始一分,最后一分。现在在新数组中分配第一个计数器的数据,然后将计数器加一。接下来分配最后一个计数器的数据并将其减一。

    int firstPointer = 0;
    int secondPointer = nums.length - 1;
    int[] newarray = new int[nums.length];
    int i = 0;
    for (i = 0; i < nums.length-1; i += 2) {
        newarray[i] = nums[firstPointer++];
        newarray[i+1] = nums[secondPointer--];
    }
    if(i<nums.length-1)
        newarray[i] = nums[firstPointer++];
于 2013-07-02T03:40:47.040 回答
1

You could sort it first, then start at the second element, swap with last element, etc.

于 2013-07-02T03:33:29.707 回答
1

试试这个,使用列表而不是数组。

public <T extends Comparable<? super T>> List<T> interleave(List<T> list) {
    final int size = list.size();
    final List<T> sorted =  new ArrayList<T>(list);
    Collections.sort(sorted);  // Now, it's sorted.
    final List<T> reversed = new ArrayList<T>(sorted);
    Collections.reverse(reversed); // Now, it's reverse sorted.
    final List<T> interleaved = new ArrayList<>();
    for (int i= 0; i < size/2; ++i) {
        interleaved.add(sorted.get(i));
        interleaved.add(reversed.get(i));
    }
    if (size % 2 == 1) {
        interleaved.add(sorted.get(size/2 + 1));
    }
    return interleaved;
}
于 2013-07-02T03:45:04.807 回答
0

也许您可以(或必须)首先对数组进行排序,然后将元素按您的顺序排列。实现 Comparable 或 Comparator 以便您可以定义数组中元素的排序方式。以下页面可能会有所帮助:在此处输入链接描述

于 2013-07-02T03:39:45.833 回答
0

这段代码进行了古怪的排序:

public static void wackySort(int[] nums) {
    Arrays.sort(nums);
    int size = nums.length;
    int[] result = new int[size];
    for (int i = 0; i < size / 2; i++) {
        result[i * 2] = nums[i];
        result[i * 2 + 1] = nums[size - 1 - i];
    }
    // one extra copy is required to handle both odd and even array sizes
    result[size - 1] = nums[size / 2];
    System.arraycopy(result, 0, nums, 0, size);
}

下面是一些测试代码:

public static void main(String[] args) {
    int[] oddSize = { 8, 6, 1, 2, 3, 80, 56 };
    wackySort(oddSize);
    System.out.println(Arrays.toString(oddSize));
    int[] evenSize = { 8, 6, 1, 2, 3, 80, 56, 5 };
    wackySort(evenSize);
    System.out.println(Arrays.toString(evenSize));
}

输出:

[1, 80, 2, 56, 3, 8, 6]
[1, 80, 2, 56, 3, 8, 5, 6]
于 2013-07-02T04:24:53.670 回答