簡體   English   中英

如何使用 Array.Sort 對數組編號進行排序

[英]How to sort array numbers using Array.Sort

我對它的工作原理有一個清晰的認識,我看過其他關於如何使用它的例子,只是不知道如何將它應用到我的代碼中。

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); 實際上是對每個迭代進行排序,但是效率很低,因為您只需要對數組排序一次,而不是1000次。 您需要放置Array.Sort(num); for循環之外並創建另一個for循環以打印排序后的數組。

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();

如果您想像原始代碼一樣全部完成此操作,則也可以使用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(); 
        }
    }
} 

產生以下輸出結果,即從左到右,這是基於我運行此命令時生成的隨機數的簡短摘要,如果將這些數字寫入文件,您將看到distinctList將具有您期望的排序值,我將顯示未排序的結果使用該代碼,並且在最后一個Console.ReadLine();檢查時,您可以在調試器中親自查看Console.ReadLine();

774 452 566 682 814 702 585 371 469 469 241 168 168 763 35 548 158 212 106 297 921 921 65 280 419 682 42 116 52 212 523 467 777 195 999 316 836 735 742 6 332 647 645 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 275 819 657 745 757 350 974 409 409 437 661 113 719 28 606 664 664 588 742 234 747 485 299 395 209 209 953 457 694 443 270 668 599 890 826 695 889 514 401 242 421 610 123 31 508 565 350253 253 206 840 535 224 673 971 26535 789520262262535535892892280280965965857769 61223223892332717179187500812136 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 127 360 874 122 385 37 173 158 889 544 334 730 433 677 103 641 425 425 57 810 773 432 358 600 426 348 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 534 801 952952 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 593 65 407 581 385 479 181 980 136 595 595 855 983 *

只是為了提示@Carl

碼:

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();
    }
}

過程是:1)制作一個數組,在制作的同時打印它(可選地,您也可以對其進行排序,但是我想它會效率很低。)2)對數組進行排序。 3)打印出已排序的數組。

您正在做什么:1)制造並排列。 2)用隨機數填充每個元素。 3)打印該隨機數。 4)對到目前為止的數組進行排序。

因此,災難就在眼前。

使用以下代碼:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM