打印数组的一部分 [英] Printing parts of an array

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

问题描述

我正在尝试运行2种方法来收集有关不同排序方法的统计信息,我有Shell排序和Gnome排序.我的问题是我必须让控制台从原始数组中打印出前20个数字.我的问题是程序对原始数组进行排序,因此,当我再次调用方法时,它们是错误的,因为对数组进行了排序,将不胜感激,这是我到目前为止尝试过的操作:
程序在这里对它进行排序:

I''m trying to run a 2 methods to gather statistics on different sorting methods, I have Shell sort and Gnome sort. My problem is that I have to have the console print out the first and last 20 numbers from the original array. My problem is that the program sorts the original array and because of this when I call the methods again they are wrong because the array has been sorted, any help would be appreciated, here''s what I tried so far:
Here''s is where the program sorts it:

int[] Number = new int[1000];  //// create a and fill a random array
          Random numb = new Random();

          for (int i = 0; i < Number.Length; i++)
          {
              Number[i] = numb.Next(100, 999);
              Console.WriteLine(Number[i]);  //// display the instances
          }
          GnomeSort(Number);
          ShellSort(Number);
          statement = 0;
          statements = 0;

          Console.WriteLine("Press any key to display the first twenty numbers");
          Console.ReadLine();
          for (int j = 0; j < 20; j++)
          {
              Console.WriteLine(Number[j]);
          }
          GnomeSort(Number);
          ShellSort(Number);


我也尝试使用两个循环:


I also tried using two loops:

for (int i = 0; i < 1000; i += 20)
{

    for (int j = i; j < i + 20; j++)
    {
        array1[j] = Number[i];
        Console.WriteLine("" + array1[j]);

    }
}


而且我还将两种不同的显示方式变成了方法.任何指向正确方向的方法都会受到赞赏,我也尝试过其他方法,例如foreach循环,我只需要知道为什么会发生这种情况以及什么是解决该问题的最佳方法.谢谢.


And I also made the two different displays into methods; any pointing in the right direction would be appreciated, I have tried other approaches too like a foreach loop, I just need to know why this is happening and what would be the best approach to fix it. Thank You.

推荐答案

不是填充单个数组并将其发送到两个排序算法,而是创建源数组,对其进行复制,然后发送复制到排序算法.完成后,再制作一个副本并将其发送给第二种排序算法.

Array.Copy [
Instead of filling a single array and sending it to both sorting algo''s, create the source array, make a copy of it, then send the copy to the sort algorithm. When that''s done, make another copy and send that to the second sort algorithm.

Array.Copy[^] documentation.


逻辑上,多次调用排序(无论哪种算法)方法都不会影响结果集.共享该程序的输出,然后告诉我您究竟要寻找什么.
Logically, calling the sorting (what ever algorithm) method multiple times should not affect the result set. Share the output of this program and tell me what exactly you are looking for.


按照上述Dave的步骤进行操作,但是请使用克隆与复制 [
Follow Dave''s steps as above, but use Array.Clone[^] instead of Copy.

Clone vs Copy[^].


这篇关于打印数组的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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