Advertisement
stefanpu

Sort array using selection sort

Feb 14th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MaxArrayElement
  8. {
  9.     class MaxArrayElement
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Write("Please ENTER index, where to start the search: ");
  14.             int n = int.Parse(Console.ReadLine());
  15.             int[] array = new int[15];
  16.  
  17.            
  18.             ArrayGenerator(array);
  19.             Console.Write("Initial array: ");
  20.             PrintArray(array);
  21.  
  22.             Console.WriteLine("The biggest int in the selected range is: " + BiggestElement(array, n));
  23.             Console.Write("\nIn ascending order: ");
  24.             //Трябва да бъде 0 вместо n за да  може да сортира целия масив, както се изисква
  25.             int[] sortedArray = AscendingOrder(array, 0);
  26.            
  27.             PrintArray(sortedArray);
  28.         }
  29.         public static int BiggestElement(int[] array, int n)
  30.         {  
  31.  
  32.             int max = int.MinValue;
  33.             for (int index = n; index < array.Length; index++)
  34.             {
  35.                 if(array[index] > max)
  36.                 {
  37.                     max = array[index];
  38.                 }
  39.             }
  40.             return max;
  41.  
  42.         }
  43.  
  44.         private static void PrintArray(int[] array)
  45.         {
  46.             for (int index = 0; index < array.Length; index++)
  47.             {
  48.                 Console.Write(array[index] + " ");
  49.             }
  50.             Console.WriteLine("\n");
  51.         }
  52.  
  53.         static int[] AscendingOrder(int[] array, int n)
  54.         {  
  55.             //Това е quicksort. Не изпозлва метода BiggestElement (или подобен)
  56.             //Array.Sort(arr2);
  57.  
  58.             List<int> listToSort = new List<int>(array);
  59.             List<int> sortedlist = new List<int>();
  60.             int currentBiggestElement;
  61.  
  62.             while (listToSort.Count > 0)
  63.             {
  64.                 currentBiggestElement = BiggestElement(listToSort.ToArray(), n);
  65.                 sortedlist.Insert(0, currentBiggestElement);
  66.                 listToSort.Remove(currentBiggestElement);
  67.             }
  68.  
  69.             return sortedlist.ToArray();
  70.  
  71.             //List<int> sortedlist = SortArrayAscending(array);
  72.            
  73.            
  74.         }
  75.  
  76.         //private static List<int> SortArrayAscending(int[] arrayToSort)
  77.         //{
  78.          
  79.         //}
  80.  
  81.         public static void ArrayGenerator(int[] array)
  82.         {
  83.             Random creator = new Random();
  84.  
  85.             for (int index = 0; index < array.Length; index++)
  86.             {
  87.                 array[index] = creator.Next(31);
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement