简体   繁体   中英

How to sort array numbers using Array.Sort

I've got a fair idea on how it works, I've seen other examples on how to use it just can't figure out how to apply it to my code.

using System;

namespace ArrayRandomNumbers
{
    class RandomNumbers
    {
        public static void Main()
        {
            int[] num = new int[1000];
            Random rnd = new Random();

            for (int i = 0; i < 1000; i++)
            {        
                num[i] = rnd.Next(1000);

                Console.WriteLine(num[i]);
                Array.Sort(num); //Doesn't do anything here
                //Console.WriteLine(num[i] = rnd.Next(1000));
            }          
            Console.ReadLine();
        }
    }
}

Array.Sort(num); actually does the sorting for each iteration, but it's inefficient because you only need to sort the array once instead of 1000 times. You need to put Array.Sort(num); outside of the for loop and create another for loop to print the sorted array.

int[] num = new int[1000];
Random rnd = new Random();

for (int i = 0; i < 1000; i++)
{        
    num[i] = rnd.Next(1000);

    Console.WriteLine(num[i]);
    //Console.WriteLine(num[i] = rnd.Next(1000));
}          

Console.WriteLine("Press Enter to sort the array");
Console.ReadLine();

// sort the array
Array.Sort(num);

// print the sorted array
for (int i = 0; i < 1000; i++)
{        
    Console.WriteLine(num[i]);
}          

Console.ReadLine();

if you want to do this all in one like your original code you could do the following as well using System;

namespace ArrayRandomNumbers
{
    class RandomNumbers
    {
        public static void Main()
        {
            int[] num = new int[1000];
            Random rnd = new Random();
            var randList = new List<int>();
            var distinctList = new List<int>();
            for (int i = 0; i < 1000; i++)
            {
                num[i] = rnd.Next(1000);
                randList.Add(num[i]);
                Console.WriteLine(randList[i]);
            }
            distinctList = randList.Distinct().ToList();
            distinctList.Sort();
            Console.ReadLine();

            distinctList.ForEach(delegate(int s)
            {
                Console.WriteLine(s.ToString());
            });
            Console.ReadLine(); 
        }
    }
} 

Yields the following output reading left to right, a short snippet based on the random numbers generated when I ran this if you write these numbers to a file you will see that the distinctList will have the sorted values as you have expected I am showing the unsorted results use the code and you can see for yourself in the debugger when you inspect at the last Console.ReadLine();

774 452 566 682 814 702 585 371 469 694 241 168 235 763 35 548 158 212 106 297 921 65 280 419 682 42 116 52 212 523 467 777 195 999 316 836 735 742 6 332 647 645 245 622 674 925 692 141 643 84 85 806 663 476 971 709 171 146 999 772 585 532 159 946 227 464 972 235 447 199 813 416 206 773 650 275 819 657 745 757 350 974 409 128 437 661 113 719 28 606 664 588 742 234 747 114 485 299 395 209 953 457 694 443 270 668 599 890 826 695 889 514 401 242 421 610 123 31 508 565 350 253 206 840 535 224 673 971 26 535 789 520 262 535 803 892 280 965 857 769 61 223 892 332 717 149 187 500 812 136 990 454 790 829 858 700 250 854 908 46 995 628 864 291 906 369 251 865 438 529 330 158 173 739 504 127 360 874 122 385 37 173 158 889 544 334 730 433 677 103 641 425 57 810 773 432 358 600 426 348 898 321 61 283 806 832 280 50 324 731 339 376 731 522 9 821 469 974 746 571 638 818 188 334 887 904 100 362 965 71 768 760 448 774 487 715 112 875 83 712 54 679 108 945 764 265 694 534 801 952 963 384 854 854 709 79 133 726 395 901 731 917 84 252 962 579 928 956 35 294 709 594 891 34 517 958 709 242 570 622 209 566 561 13 108 384 905 593 65 407 581 385 479 181 980 136 595 855 983*

just for hint to @Carl

Code:

class Program
    {
        static void Main(string[] args)
        {
        var num = new int[1000];
        var rnd = new Random();

        Console.WriteLine("Before Sorting...");
        for (var i = 0; i < 1000; i++)
        {
            num[i] = rnd.Next(1000);

            Console.WriteLine(num[i]);
             //Doesn't do anything here
            //Console.WriteLine(num[i] = rnd.Next(1000));
        }
        Console.WriteLine("After Sorting...");
        Array.Sort(num);

        for (var i = 0; i < 1000; i++)
        {

            Console.WriteLine(num[i]);
            //Doesn't do anything here
            //Console.WriteLine(num[i] = rnd.Next(1000));
        }

        Console.ReadLine();
    }
}

Process is: 1) make an array, Print it while making (optionally you can sort it too, but it will be inefficient, i guess.) 2) sort the array. 3) Print out Sorted array.

What you are doing: 1) Make and array. 2) Populate each element with random number. 3) Printing that random number. 4) sorting the array so far made.

hence the Disaster, you are seeing.

Use below Code:

int[] intArray = new int[] {1, 9, 6, 7, 5, 9};

// Sort array in ascending order
Array.Sort(intArray);
Console.WriteLine("Ascending: ");
foreach(int value in intArray)
{
  Console.Write(value + " ");
}

// Reverse array
Array.Reverse(intArray);
Console.WriteLine("\n\nDescending: ");
foreach(int value in intArray)
{
   Console.Write(value + " ");
}

Output:

Ascending: 
1 2 3 4 5 7 9

Descending: 
9 7 5 4 3 2 1 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM