Click here to Skip to main content
14,328,292 members
Rate this:
Please Sign up or sign in to vote.
See more:
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]);

    }
}

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.
Posted
Comments
Sergey Alexandrovich Kryukov 12-Mar-12 19:44pm
   
You explained how it goes wrong, now, could you explain how should it behave to appear right.
Why using GnomeSort and ShellSort? How about using sorted list to keep it sorted all the time?
What are the sorting criteria? Do you need to preserve original order? What different orders do you need to have?
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   
Comments
Rajeev Jayaram 13-Mar-12 5:09am
   
Good suggestion. But, using Clone instead of Copy would be more appropriate. See my answer.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Follow Dave's steps as above, but use Array.Clone[^] instead of Copy.

Clone vs Copy[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100