• Top Posts

    C# program to sort the array with selection sort.


     using System;  
     using System.Collections.Generic;  
     using System.Linq;  
     using System.Text;  
     using System.Threading.Tasks;  
     namespace sorting  
     {  
       class Program  
       {  
         static void Main(string[] args)  
         {  
           var generator = new Random();  
           var data = new int[10];  
           // fill array with random ints in range 10-99  
           for (var i = 0; i < data.Length; ++i)  
           {  
             data[i] = generator.Next(10, 100);  
           }  
           Console.WriteLine("Unsorted array:");  
           Console.WriteLine(string.Join(" ", data) + "\n"); // display array  
           SelectionSort(data); // sort array  
           Console.WriteLine("Sorted array:");  
           Console.WriteLine(string.Join(" ", data) + "\n"); // display array  
           Console.ReadLine();  
         }  
         public static void SelectionSort(int[] values)  
         {  
           // loop over data.Length - 1 elements  
           for (var i = 0; i < values.Length - 1; ++i)  
           {  
             var smallest = i; // first index of remaining array  
                      // loop to find index of smallest element  
             for (var index = i + 1; index < values.Length; ++index)  
             {  
               if (values[index] < values[smallest])  
               {  
                 smallest = index;  
               }  
             }  
             Swap(ref values[i], ref values[smallest]); // swap elements  
             PrintPass(values, i + 1, smallest); // output pass of algorithm  
           }  
         }  
         public static void Swap(ref int first, ref int second)  
         {  
           var temporary = first; // store first in temporary  
           first = second; // replace first with second  
           second = temporary; // put temporary in second  
         }  
         public static void PrintPass(int[] values, int pass, int index)  
         {  
           Console.Write($"after pass {pass}: ");  
           for (var i = 0; i < index; ++i)  
           {  
             Console.Write($"{values[i]} ");  
           }  
           Console.Write($"{values[index]}* "); // indicate swap  
                              // finish outputting array  
           for (var i = index + 1; i < values.Length; ++i)  
           {  
             Console.Write($"{values[i]} ");  
           }  
           Console.Write("\n       "); // for alignment  
                             // indicate amount of array that is sorted  
           for (var j = 0; j < pass; ++j)  
           {  
             Console.Write("-- ");  
           }  
           Console.WriteLine("\n");  
         }  
       }  
     }  
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728