Csharp/C Sharp/Collections Data Structure/Array Sort — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:39, 26 мая 2010

Sorts and array in ascending order, then reverses the elements

<source lang="csharp"> // Sort.cs -- Sorts and array in ascending order, then reverses the elements

   using System;
   
   public class Sort
   {
       static public void Main ()
       {
            DateTime now = DateTime.Now;
            Random rand = new Random ((int) now.Millisecond);
            int [] Arr = new int [12];
            for (int x = 0; x < Arr.Length; ++x)
            {
                Arr [x] = rand.Next () % 101;
            }
            Console.WriteLine ("The unsorted array elements:"); 
            foreach (int x in Arr)
            {
                Console.Write (x + " ");
            }
            Array.Sort (Arr);
            Console.WriteLine ("\r\n\r\nThe array sorted in ascending order:"); 
            foreach (int x in Arr)
            {
                Console.Write (x + " ");
            }
            Array.Reverse (Arr);
            Console.WriteLine ("\r\n\r\nThe array sorted in descending order:"); 
            foreach (int x in Arr)
            {
                Console.Write (x + " ");
            }
       }
   }


      </source>


System.Array: Sorting and Searching

<source lang="csharp"> using System; public class SortingandSearchingArray {

   public static void Main()
   {
       int[]    arr = {5, 1, 10, 33, 100, 4};
       
       Array.Sort(arr);
       foreach (int v in arr)
       Console.WriteLine("Element: {0}", v);
   }

}

      </source>