Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用带负值的计数排序?(降序)_C#_Sorting_Counting Sort - Fatal编程技术网

C# 使用带负值的计数排序?(降序)

C# 使用带负值的计数排序?(降序),c#,sorting,counting-sort,C#,Sorting,Counting Sort,对于x>0,我有一个计数排序,它按降序对数组进行排序。然而,在考虑负数时,我的实现的逻辑会崩溃,因为我在helper数组值中使用负索引。我想用uint但是我不太熟悉 我怎么能用计数法来计算呢 static void countingSort(int[]arr) { int i,j,max=-1;//我想它在这附近散开了 int[]值; 对于(i=0;imax)max=arr[i]; 值=新整数[max+1]; //这里,当i=2时,它达到负指数,寻找-6。 对于(i=0;i

对于
x>0
,我有一个计数排序,它按降序对数组进行排序。然而,在考虑负数时,我的实现的逻辑会崩溃,因为我在helper数组
值中使用负索引。我想用uint但是我不太熟悉

我怎么能用计数法来计算呢

static void countingSort(int[]arr)
{
int i,j,max=-1;//我想它在这附近散开了
int[]值;
对于(i=0;imax)max=arr[i];
值=新整数[max+1];
//这里,当i=2时,它达到负指数,寻找-6。
对于(i=0;i0)
{
价值观[j]——;
arr[i]=j;
i++;
}
否则j--;
}
}
我知道我的问题在于帮助数组的索引。由于我不想继续制作带有负索引的数组,我有点不知所措

你甚至可以在c#中这样做,而不实现整个类作为索引器吗? 我知道你可以用C来做,它定义得很好:

根据C99§6.5.2.1/2: 下标运算符[]的定义是E1[E2]与(*(E1)+(E2))相同

我的测试数组是
{8,5,-6,7,1,4}

我的预期输出是
{8,7,5,4,1,-6}

参见此。

问题在于“值[arr[i]]++”。如果arr[i]中的值返回负数,则该值将不起作用

一种可能是编写一个函数“Hash key(arr[i]),它接受数组中的任何数字,并将其转换为字典键值。您可以自己编写或使用库

如果您使用的是C#,那么“System.Collections”有许多类来帮助建立字典索引

using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

   // Creates and initializes a new ArrayList.
   ArrayList myAL = new ArrayList();
   myAL.Add("Hello");
   myAL.Add("World");
myAL.加上(“!”); 您的助手将加载源“arr”数组中的数据。
希望这能有所帮助。

实际上,它可以在一个非常简单的转换中完成。这里是您的排序更新:

    static void countingSort(int[] arr)
    {
        int i, j, max = -1; 
        int[] values;
        //get the highest absolute value to determine the size of the array 
        for (i = 0; i < arr.Length; i++)
            if (Math.Abs(arr[i]) > max) max = Math.Abs(arr[i]);
        //create double the max size array
        values = new int[max*2 + 1];

        //when reaching a value make a shift of max size            
        for (i = 0; i < arr.Length; i++)
            values[arr[i]+max]++;

        i = 0; j = values.Length - 1;
        while (i < arr.Length)
        {
            if (values[j] > 0)
            {
                values[j]--;
                //shift back when putting in the sorted array
                arr[i] = j-max;
                i++;
            }
            else j--;
        }

    }
static void countingSort(int[]arr)
{
int i,j,max=-1;
int[]值;
//获取最高绝对值以确定数组的大小
对于(i=0;imax)max=Math.Abs(arr[i]);
//创建两倍于最大大小的数组
值=新整数[max*2+1];
//当达到某个值时,移动最大大小
对于(i=0;i0)
{
价值观[j]——;
//放入排序数组时向后移动
arr[i]=j-max;
i++;
}
否则j--;
}
}

这样,假设你有一个{4,3,2}数组,你将创建一个大小为(4*2+1)=9的数组,因为最高的绝对值是-4,移位将是4,所以-4将位于索引0中,例如2将位于值数组的索引6中,这样你就避免了问题并得到了你想要的结果。

在你的例子中,您已经在扫描输入数组以查找最大值。缺少的是,您没有扫描输入数组以找到最小值。如果添加该值,然后知道最小值,则可以偏移数组索引的范围以允许负数(如果只处理正数,甚至可能减少数组的大小)

看起来是这样的:

static void Sort(int[] array)
{
    int min = int.MaxValue, max = int.MinValue;

    for (int i = 0; i < array.Length; i++)
    {
        if (array[i] < min)
        {
            min = array[i];
        }

        if (array[i] > max)
        {
            max = array[i];
        }
    }

    int[] counts = new int[max - min + 1];

    for (int i = 0; i < array.Length; i++)
    {
        counts[array[i] - min]++;
    }

    int k = 0;

    for (int j = max; j >= min; j--)
    {
        for (int i = 0; i < counts[j - min]; i++)
        {
            array[k++] = j;
        }
    }
}
static void Sort(int[] array)
{
    int min = int.MaxValue, max = int.MinValue;

    Dictionary<int, int> counts = new Dictionary<int, int>();

    for (int i = 0; i < array.Length; i++)
    {
        if (array[i] < min)
        {
            min = array[i];
        }

        if (array[i] > max)
        {
            max = array[i];
        }

        int count;

        // If the key is not present, count will get the default value for int, i.e. 0
        counts.TryGetValue(array[i], out count);
        counts[array[i]] = count + 1;
    }

    int k = 0;

    for (int j = max; j >= min; j--)
    {
        int count;

        if (counts.TryGetValue(j, out count))
        {
            for (int i = 0; i < count; i++)
            {
                array[k++] = j;
            }
        }
    }
}
嗯,C#不像C那样在索引器中做“指针数学”,所以它们不是等价的。
static void Sort(int[] array)
{
    int min = int.MaxValue, max = int.MinValue;

    Dictionary<int, int> counts = new Dictionary<int, int>();

    for (int i = 0; i < array.Length; i++)
    {
        if (array[i] < min)
        {
            min = array[i];
        }

        if (array[i] > max)
        {
            max = array[i];
        }

        int count;

        // If the key is not present, count will get the default value for int, i.e. 0
        counts.TryGetValue(array[i], out count);
        counts[array[i]] = count + 1;
    }

    int k = 0;

    for (int j = max; j >= min; j--)
    {
        int count;

        if (counts.TryGetValue(j, out count))
        {
            for (int i = 0; i < count; i++)
            {
                array[k++] = j;
            }
        }
    }
}