Advertisement
Booster

BiggestElementInArrayByIndexToEnd SortAscAndDecs

Aug 7th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System;
  2.  
  3. class FindBiggestElementInArray
  4. {
  5.     static void PrintSortedArray(int[] array)
  6.     {
  7.         foreach (var item in array)
  8.         {
  9.             Console.Write(item + " ");
  10.         }
  11.     }
  12.     static int FindBiggestElement(int[] array, int start)
  13.     {
  14.         int indexMax = 0;
  15.         int max = Int32.MinValue;
  16.         for (int i = start; i < array.Length; i++)
  17.         {
  18.             if (array[i] > max)
  19.             {
  20.                 max = array[i];
  21.                 indexMax = i;
  22.             }
  23.         }
  24.         return indexMax;
  25.     }
  26.     static int[] ChangeElements(int[] array, int firstElement, int secElement)
  27.     {
  28.         int tempElement = array[firstElement];
  29.         array[firstElement] = array[secElement];
  30.         array[secElement] = tempElement;
  31.  
  32.         return array;
  33.     }
  34.     static int[] SortDescending(int[] array)
  35.     {
  36.         for (int i = 0; i < array.Length; i++)
  37.         {
  38.             ChangeElements(array, FindBiggestElement(array, i), i);
  39.         }
  40.         return array;
  41.     }
  42.     static int[] SortAscending(int[] array)
  43.     {
  44.         SortDescending(array);
  45.         for (int i = 0; i < (array.Length/2); i++)
  46.         {
  47.             ChangeElements(array, array.Length - i - 1, i);
  48.            
  49.         }
  50.         return array;
  51.     }
  52.     static void Main()
  53.     {
  54.         Console.Write("Enter array lenght: ");
  55.         int arrLenght = int.Parse(Console.ReadLine());
  56.         int[] array = new int[arrLenght];
  57.  
  58.         for (int i = 0; i < arrLenght; i++)
  59.         {
  60.             array[i] = int.Parse(Console.ReadLine());
  61.         }
  62.         Console.WriteLine("Enter start index");
  63.         int start = int.Parse(Console.ReadLine());
  64.         Console.WriteLine("Biggest number is: {0}", array[FindBiggestElement(array, start)]);
  65.  
  66.         Console.WriteLine("Press 1 for ascending sort\nPress 0 for descending sort:");
  67.         int typeSort = int.Parse(Console.ReadLine());
  68.  
  69.         if (typeSort == 1)
  70.         {
  71.             PrintSortedArray(SortAscending(array));
  72.         }
  73.         else
  74.         {
  75.             PrintSortedArray(SortDescending(array));
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement