Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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#_Arrays_Sorting_Mergesort_Insertion Sort - Fatal编程技术网

C# 按排序顺序将元素插入数组

C# 按排序顺序将元素插入数组,c#,arrays,sorting,mergesort,insertion-sort,C#,Arrays,Sorting,Mergesort,Insertion Sort,我想做的是,当我从用户那里获取输入时,将该输入按排序顺序插入数组,例如用户输入22,3,9,10,33 产量为:3,9,10,22,33 我下面的代码是有效的,除了添加的最后一个元素的索引错误之外。这是一个学校的测试(这就是为什么数组有50个元素大,整个班级都有getter和setter&缺少错误检查),我试图找出哪里出了问题,我尝试了插入排序和选择排序,两者都产生了这个结果。根据我的理解,它应该是45个连续的零,然后我的元素按升序排列 这是我得到的输出(无论我在调用打印方法后是使用选择排序还是

我想做的是,当我从用户那里获取输入时,将该输入按排序顺序插入数组,例如用户输入22,3,9,10,33
产量为:3,9,10,22,33

我下面的代码是有效的,除了添加的最后一个元素的索引错误之外。这是一个学校的测试(这就是为什么数组有50个元素大,整个班级都有getter和setter&缺少错误检查),我试图找出哪里出了问题,我尝试了插入排序和选择排序,两者都产生了这个结果。根据我的理解,它应该是45个连续的零,然后我的元素按升序排列

这是我得到的输出(无论我在调用打印方法后是使用选择排序还是插入排序)

排序数组:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 9 10 22

 public class test
{
    private int [] arr;
    private int maxSize;
    private int numItems;
    public test(int maxSize)
    {
        this.maxSize = maxSize;
        numItems = 0;
        arr = new int[maxSize];
    }

    public bool addItem(int key)
    {
        if (numItems < maxSize)
        {
            selectionSort(key);
            arr[numItems] = key;
            numItems++;
            return true;
        }
        return false;
    }

    public bool insertionSort(int key)
    {
        int n = arr.Length - 1;
        for (int i = 1; i < n; i++)
        {
            key = arr[i];
            int j = i - 1;
            while(j>=0 && arr[j] > key)
            {
                arr[j+1] = arr[j];
                j --;
            }
            arr[j + 1] = key;
        }
        return true;
    }

    public bool selectionSort(int key)
    {
        int  n = arr.Length - 1;
        for (int i = 0; i < n; i++)
        {
            key = i;
            for(int j = i + 1; j < n; j++)
            {
                if (arr[j] < arr[key])
                {
                    key = j;
                }
            }
            int temp = arr[key];
            arr[key] = arr[i];
            arr[i] = temp;

        }
        return true;
    }    

         static void Main(string[] args)
    {

        test x = new test(50);

        int count = 0;
        int element;
        while (count < 5)
        {
            Console.WriteLine("Enter an element to add into the array");
            element = Convert.ToInt32(Console.ReadLine());
            x.addItem(element);
            count++;
        }}
公共类测试
{
私有int[]arr;
私有int-maxSize;
私有内部网络;
公共测试(int-maxSize)
{
this.maxSize=maxSize;
numItems=0;
arr=新整数[maxSize];
}
公共布尔附加项(整数键)
{
if(numItems=0&&arr[j]>键)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=键;
}
返回true;
}
公共布尔选择排序(整数键)
{
int n=阵列长度-1;
对于(int i=0;i
我想您正在寻找插入排序,这里是一个示例

   static void Main(string[] args)
    {
        int[] numbers = new int[10] {22,1,34,20,12,10,5,33,11,5};
        Console.WriteLine("\nOriginal Array Elements :");
        Show(numbers);
        Console.WriteLine("\nSorted Array Elements :");
        Show(InsertionSort(numbers));
        Console.ReadKey();
        }

    static int[] InsertionSort(int[] inputArray)
    {
        for (int i = 0; i < inputArray.Length - 1; i++)
        {
            for (int j = i + 1; j > 0; j--)
            {
                if (inputArray[j - 1] > inputArray[j])
                {
                    int temp = inputArray[j - 1];
                    inputArray[j - 1] = inputArray[j];
                    inputArray[j] = temp;
                }
              }
        }
        return inputArray;         
    }
    public static void Show(int[] array)
    {
        foreach (int i in array)
        {
            Console.Write(i.ToString() + "  ");
        }
     }
static void Main(字符串[]args)
{
int[]数字=新的int[10]{22,1,34,20,12,10,5,33,11,5};
Console.WriteLine(“\n原始数组元素:”);
显示(数字);
Console.WriteLine(“\n被忽略的数组元素:”);
显示(插入排序(数字));
Console.ReadKey();
}
静态int[]插入排序(int[]输入数组)
{
for(inti=0;i0;j--)
{
if(输入阵列[j-1]>输入阵列[j])
{
int temp=输入阵列[j-1];
输入阵列[j-1]=输入阵列[j];
输入阵列[j]=温度;
}
}
}
返回输入;
}
公共静态无效显示(int[]数组)
{
foreach(数组中的int i)
{
Console.Write(i.ToString()+);
}
}

我想您正在寻找插入排序,这里是一个示例

   static void Main(string[] args)
    {
        int[] numbers = new int[10] {22,1,34,20,12,10,5,33,11,5};
        Console.WriteLine("\nOriginal Array Elements :");
        Show(numbers);
        Console.WriteLine("\nSorted Array Elements :");
        Show(InsertionSort(numbers));
        Console.ReadKey();
        }

    static int[] InsertionSort(int[] inputArray)
    {
        for (int i = 0; i < inputArray.Length - 1; i++)
        {
            for (int j = i + 1; j > 0; j--)
            {
                if (inputArray[j - 1] > inputArray[j])
                {
                    int temp = inputArray[j - 1];
                    inputArray[j - 1] = inputArray[j];
                    inputArray[j] = temp;
                }
              }
        }
        return inputArray;         
    }
    public static void Show(int[] array)
    {
        foreach (int i in array)
        {
            Console.Write(i.ToString() + "  ");
        }
     }
static void Main(字符串[]args)
{
int[]数字=新的int[10]{22,1,34,20,12,10,5,33,11,5};
Console.WriteLine(“\n原始数组元素:”);
显示(数字);
Console.WriteLine(“\n被忽略的数组元素:”);
显示(插入排序(数字));
Console.ReadKey();
}
静态int[]插入排序(int[]输入数组)
{
for(inti=0;i0;j--)
{
if(输入阵列[j-1]>输入阵列[j])
{
int temp=输入阵列[j-1];
输入阵列[j-1]=输入阵列[j];
输入阵列[j]=温度;
}
}
}
返回输入;
}
公共静态无效显示(int[]数组)
{
foreach(数组中的int i)
{
Console.Write(i.ToString()+);
}
}

我发现您的代码有两个问题,这些问题很小

  • 更新
    addItem
    功能,将
    selectionSort
    移动到分配操作下方

  • 这应该可以解决您的问题。

    我发现您的代码有两个问题,这些问题很小

  • 更新
    addItem
    功能,将
    selectionSort
    移动到分配操作下方

  • 这将解决您的问题。

    可能重复:您必须使用数组吗?:)可能重复:您必须使用数组吗?:)这就是解决方案!谢谢,我感觉它与addItem方法中的赋值操作有关,但我不知道如何打破循环并一直到数组的末尾。谢谢您指出这就是解决方案!谢谢您,我有一种感觉g它与addItem方法中的赋值操作有关,但我不知道如何打破循环并一直到数组的末尾。谢谢您指出这一点
            public bool selectionSort(int key)
        {
            int n = arr.Length - 1;
            for (int i = 0; i < n; i++)
            {
                key = i;
                for (int j = i + 1; j <= n; j++) // <= n instead of < n
                {
                    if (arr[j] < arr[key])
                    {
                        key = j;
                        break;             // break here once you have k = j.
                    }
                }
                int temp = arr[key];
                arr[key] = arr[i];
                arr[i] = temp;
    
            }
            return true;
        }