954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Stuck with sorting a Random code in C#!! need help!!

My problem is, I've just started with C# and i was given a problem to solve by my teacher... here is the code...

{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[20];
            Random rand = new Random();
            for (int i = 0; i < 10; i++) a[i] = rand.Next(10000);
            for (int i = 0; i < 10; i++) Console.WriteLine(a[i]);

            Console.Read();
        }
    }
}


Now i have to use a Loop to desplay the number from small to big. The way it is now is the numbers display random between 0-10000 and i was told that i need to add a loop/s to that code that display the unsorted number from min to max, in other words, sorted. both unsorted and sorted must be displayed in the console. I've been trying to figure this out for the past 6hours and no luck, not even on the internet. Please help!!!!

frost1
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

You can use built-in Sort method

// Declare an array with ten elements
//int[] a = new int[20];
int[] a = new int[10];

Random rand = new Random();
for (int i = 0; i < 10; i++) a[i] = rand.Next(10000);
for (int i = 0; i < 10; i++) Console.WriteLine(a[i]);
// Sort array
Array.Sort(a);
// Dump values
for (int i = 0; i < 10; i++) Console.WriteLine(a[i]);

Console.Read();


Now it's important not to declare too "big" array because elements have the default value 0 and they would be sorted too :)

HTH

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

If I understand you, you want to get 20 different numbers (or what ever) using a Random class. If you will only use random.Next method, it possible of duplications too you know.
Ok, next thing, you have to show all 20 numbers in the order they were inserted into an array, and the same 20 numbers sorted.

So lets do this code:

Random r = new Random();
int[] array = new int[20];
for(int i = 0; i < array.Lenght; i++)
{
    while(true)
    {
         int num = r.Next(1, 10001);
         if(!array.Contains(num)
         {
              array[i] = num;
              break;
         }
    }
}
Console.WriteLine("Unsorted numbers are: {0}.", String.Join(",", array.Select(s => s.ToString()).ToArray()));
Array.Sort(array); //sorting array!
Console.WriteLine("Sorted numbers are: {0}.", String.Join(",", array.Select(s => s.ToString()).ToArray()));


Last part is using Linq, to display an array, using Join method. Inside of it I used a Linq (lambda expressions) to join numbers, which must be strings it you want to use Join method).

Not big deal.

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

Array.Sort is by far the best method to use. If, however, this is a programming exercise of manually sorting a numeric array, then, you will need two separate arrays. One will hold your original data, and one will hold your sorted list.

In pseudocode, you need to perform something similar to the following.

Bool finished = false;
int sortLength = Original Array length;
while not finished
{
   set finished to true (we assume the array is already sorted)
   loop from the beginning of the array to the array position of sortLength incrementing by 1
   {
      check the if the value at the current array position is greater than the value in the next position
      if it is, copy the current position to a temporary variable and set the current value to the value at the next position. Then, copy the temporary value to the value at the next position and set "finished" to false.
   }
   once the for loop is over, decrement the "sortLength" by 1.
   (We already know that the last item in the array is the largest, if we continue sorting, we already know that these numbers are larger as we are pushing them down the list)

if finished is true at this point, the list is sorted
}
Print the list


If you want a bit more technical explanation, this type of sorting is called "Bubble Sort". There are ways of optimising this, but I believe this will get done what you need to do. http://en.wikipedia.org/wiki/Bubblesort

Ketsuekiame
Master Poster
752 posts since May 2010
Reputation Points: 349
Solved Threads: 107
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: