简体   繁体   English

对数组进行排序,以便第一个和最后一个元素形成一个“对”

[英]sort an array, so that first and last element will form a “pair”

I have an array of objects which I want to sort by indices in the following order. 我有一个对象数组,想要按以下顺序按索引排序。 I will always have an array size to the power of 2. 我将始终拥有2的幂的数组大小。

Example array size: 8. Indices: [0][1] [2][3] [4][5] [6][7] 数组大小示例:8.索引: [0][1] [2][3] [4][5] [6][7]

After sort: [0][7] [1][6] [2][5] [3][4] 排序后: [0][7] [1][6] [2][5] [3][4]

So basically alternating between first and last element which was not sorted yet. 所以基本上在第一个和最后一个尚未排序的元素之间交替。

I already thought of a way of doing it, and I can get the "pairs" but they are in the wrong order (and I think it would not work with any other to the power of 2 array?). 我已经想过这样做的方法,我可以得到“对”,但是它们的顺序不正确(而且我认为它不能与2阵列的功能一起使用吗?)。 Here I'm using an int array and their indices as values to make it simpler for myself. 在这里,我使用一个int数组及其索引作为值,以使其对我自己更简单。

int[] array = new int[]{0,1,2,3,4,5,6,7};
int[] sortedArray = new int[8];
for(int i = 0; i < array.length; i+=2){
    sortedArray[i] = array[i];
}
for(int i = 1; i < array.length; i+=2){
    sortedArray[i] = array[array.length - i];
}

Output: [0][7] [2][5] [4][3] [6][1] 输出: [0][7] [2][5] [4][3] [6][1]

You can do this with a single loop. 您可以单循环执行此操作。 Consider the following algorithm: 考虑以下算法:

int[] array = new int[]{0,1,2,3,4,5,6,7};
int[] sortedArray = new int[8];
for(int i = 0; i < array.length; i+=2){
    sortedArray[i] = array[i/2];
    sortedArray[i + 1] = array[array.length - i/2 - 1];
}
System.out.println(Arrays.toString(sortedArray)); // prints [0, 7, 1, 6, 2, 5, 3, 4]

This creates the final array by setting two values at a time: 通过一次设置两个值来创建最终数组:

  • every even index of the result array is mapped with the first values of the initial array 结果数组的每个偶数索引都映射有初始数组的第一个值
  • every odd index of the result array is mapped with the last values of the initial array 结果数组的每个奇数索引都将映射到初始数组的最后一个值

Here's a recursive approach to it, and improvements exist. 这是一种递归方法,并且存在改进之处。

Motivation : Work your way through the array until you're left with only two values to compare. 动机 :遍历数组,直到只剩下两个要比较的值。 Once you're done, simply print them. 完成后, 只需打印它们即可。 Otherwise, print the value and continue slicing the array. 否则,打印该值并继续切片该数组。 We use two index locations to help us walk through the array, as each one governs its end of the array. 我们使用两个索引位置来帮助我们遍历数组,因为每个索引位置都控制着数组的末端。 We cease recursion when the difference between our start and end index is 1. 当起始索引和终止索引之间的差为1时,我们将停止递归。

Steps to guard against silly things, like a zero-length array, I leave as an exercise for the reader. 谨防读者采取类似零长度数组之类的愚蠢措施的步骤。

public static void arrangeArray(int[] array) {
    arrangeArray(array, 0, array.length - 1);
}

private static void arrangeArray(int[] array, int startIndex, int endIndex) {
    System.out.printf("[%d][%d]\t", array[startIndex], array[endIndex]);
    if(endIndex - startIndex > 1) {
        arrangeArray(array, startIndex + 1, endIndex - 1);
    }
}

You can extend a List and then Override the way this List works with indices so 0 remains 0 but 1 becomes physically a 2 in your internal array. 您可以扩展一个List ,然后Override此List与索引的工作方式,因此0仍为01在内部数组中实际上变为2

If you implement this object correctly, Collections.sort() won't see the difference. 如果正确实现此对象, Collections.sort()将看不到区别。 Then you can add your methods to get the internal Array for whatever you have to do. 然后,您可以添加方法以获取需要执行的内部数组。

The advantage of this approach is performance. 这种方法的优点是性能。 You don't have to scramble the list yourself in a secondary step. 您不必在第二步中自己打乱列表。

class swap
{
public static void main(String args[])
{
int arr[]={0,1,2,3,4,5,6,7};
int sorted[]=new int[8];
int end=7;
int i=1,j;
for(int k=0;k<8;k++)
{
sorted[k]=arr[k];
}
for(j=1;j<8;j=j+2)
{
sorted[j]=arr[end];
end--;
}
for(j=2;j<8;j=j+2)
{
sorted[j]=arr[i];
i++;
}
for(int k=0;k<8;k++)
System.out.println(sorted[k]);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将第一个数组元素与最后一个交换,第二个与倒数第二个交换,依此类推 - Swapping first array element with last, second with second last and so on 如何比较首个元素和最后一个元素以降序对数组进行排序 - How to sort an array in descending order comparing the first element and the last 堆排序未对数组的最后一个和第一个元素进行排序 - Heap-Sort not sorting the last and first element of the array 排除数组的第一个和最后一个元素 - Excluding the first and last element of array 我想找到数组的第一个和最后一个元素和第二个和第二个最后一个元素以及第三个和第三个最后一个元素的总和,依此类推 - i want to find sum of array first and last element and 2nd and 2nd last element and 3rd and 3rd last element and so on 如何按名字,姓氏等对人员列表进行排序? - How to sort a List of Persons by first name, last name and so on? Shell排序未对数组的第一个元素进行排序 - Shell sort is not sorting the first element of an array 数组的最后一个元素在迭代时首先打印 - last element of the array gets printed first on iteration 如何在java中获取数组中的第一个和最后一个元素? - How to get first and last element in an array in java? 给定2个数组,如何按升序对第一个数组进行排序,并交换第二个数组的值以便与第一个数组匹配? - Given 2 arrays, how to sort the first array in ascending order, and swap the second array values so as to match with the first array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM