简体   繁体   English

C#使用指针划分整数数组

[英]C# dividing an integer array using pointer

So I have a quicksort algorithm, i have to divide my integer array in to two, and call both arrays back recursively. 所以我有一个快速排序算法,我必须将我的整数数组分成两个,并递归地调用两个数组。

partition(array,lower);
partition(&array[lower + 1], array.Length - lower - 1);

But c# won't accept that code, even though i tried adding unsafe, to partition class, so i wonder what must be done for that to work? 但是c#不会接受那个代码,即使我尝试将不安全的东西添加到分区类中,所以我想知道必须要做些什么才能工作呢?

maybe i could fix it without using pointers, but i want to learn so every time i have a problem like that i wont have to change the whole code for it. 也许我可以修复它而不使用指针,但我想学习所以每次我有一个问题,我不必更改它的整个代码。

How about linq? linq怎么样?

array.Take(lower);
array.Skip(lower);

You'll love it 你会爱上它的

Easy way to create SubArray out of Array Array创建SubArray简便方法

    private static void Main()
    {

        int[] array = Enumerable.Range(1, 10).ToArray();
        int lower = 5;
        int[] array1 = array.SubArray(0, lower);
        int[] array2 = array.SubArray(lower, array.Length - lower);
    }

    public static T[] SubArray<T>(this T[] source, int sourceIndex, int length)
    {
        T[] result = new T[length];
        Array.Copy(source, sourceIndex, result, 0, length);
        return result;
    }

To use unsafe code, you need 2 things: 要使用不安全的代码,您需要两件事:

  1. Mark unsafe code with the unsafe keyword (as you said you're doing) 使用unsafe关键字标记不安全的代码(正如您所说的那样)
  2. Compile the project as unsafe by configuring the appropriate setting 通过配置适当的设置将项目编译为不安全

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM