C # to achieve sorting code detailed explanation

Time:2021-7-22

C # sort case code

using System;

Case study of namespace sorting
{
  class Program
  {
    static void Main(string[] args)
    {
      //Define random sequence
      int a, b, c, d;
      Random rand = new Random();
      int[] intArray = new int[10];
      for (int i = 0; i < intArray.Length; i++)
      {
        a = rand.Next(1, 100);
        intArray[i] = a;
      }
      foreach (int item in intArray)
      {
        Console.Write("{0}\t", item);
      }
      Console.WriteLine("\n");

      //Directly use the existing method to sort the sequence in ascending order
      Array.Sort(intArray);
      foreach (int item in intArray)
      {
        Console.Write("{0}\t", item);
      }
      Console.WriteLine("\n");
      //Using bubble sort method to sort the sequence in reverse order manually
      for (int a1 = 0; a1 < intArray.Length; a1++)
      {
        for (int a2 = a1 + 1; a2 < intArray.Length; a2++)
        {
          if (intArray[a1] < intArray[a2])
          {
            int temp = intArray[a1];
            intArray[a1] = intArray[a2];
            intArray[a2] = temp;
          }
        }
      }
      foreach (int item in intArray)
      {
        Console.Write("{0}\t", item);
      }
      Console.WriteLine("\n");
      //Direct use of existing methods to reverse sort the sequence (invert data elements)
      Array.Reverse(intArray);
      foreach (int item in intArray)
      {
        Console.Write("{0}\t", item);
      }
      Console.WriteLine("\n");

    }
  }
}

Results after operation:

The above is all the knowledge points of this introduction. Thank you for your study and support for developer.