Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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

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.
 
Share this answer
 
Comments
Rajeev Jayaram 13-Mar-12 5:09am    
Good suggestion. But, using Clone instead of Copy would be more appropriate. See my answer.
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.
 
Share this answer
 
Follow Dave's steps as above, but use Array.Clone[^] instead of Copy.

Clone vs Copy[^].
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900