如何移动数组元素 [英] How to shift array elements

查看:166
本文介绍了如何移动数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有N = 32个项目有正负值的数组。前N / 2个元素是积极的,排序值和第二n / 2个元素为负,而排序价值。我想整个阵列按值进行排序,从最小的负值最大的正数,这意味着,如果有32个元素的第16(N / 2)来分类的内容应包含原始数组的第二个16元素的值开始和排序阵列的第二16个元件应包含原始数组的第16个值

I have an array of n = 32 items with positive and negative values. First n/2 elements are positive and sorted by value and second n/2 elements are negative and sorted by value as well. I would like to sort the whole array by value, starting from the smallest negative value to biggest positive value which means if there are 32 elements the first 16 (n/2) sorted elements should contain the values of second 16 elements of the original array and the second 16 elements of the sorted array should contain the first 16 values of the original array.

假设的例子:

double[] original = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, -16, -15, ..., -1};

double[] sorted = {-16, -15, ...., -1, 1, 2, ..., 16};

有谁知道什么是移动的元素,以生成原始排序的数组的最佳方法是什么?

Does anyone know what is the best way to shift the elements to generate the sorted array from original?

此阵列是由连接到不具有的元素按大小排序以同样的方式,并具有要移位相同的方式原来以使阵列不应由大小进行排序另一个阵列的方式,的它已被移位

This array is by the way tied to another array which doesn't have the elements sorted by size the same way and has to be shifted the same way as original so the array shouldn't be sorted by size, it has to be shifted.

推荐答案

所以,你想要的第二阵列,与原来的,但在位置的内容转移?要么做明确的:

So you want a second array, with the contents of the original but at locations shifted? Either do it explicitly:

double[] result = new double[32];
for (int i=0; i < 32; i++)
{
    result[(i+16)%32] = original[i];
}

或者使用 Array.Copy 两次:

double[] result = new double[32];
Array.Copy(original, 0, result, 16, 16);
Array.Copy(original, 16, result, 0, 16);

这篇关于如何移动数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆