[英]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: 通过一次设置两个值来创建最终数组:
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
仍为0
但1
在内部数组中实际上变为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.