Advertisement
Asenval

Untitled

Jan 27th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using System;
  2.  
  3. class MaxElementInPortionalArray
  4. {
  5.     static void Main()
  6.     {
  7.         int[] arr = new int[] {2,5,6,9,12,5,3,8,7,6,9,10,5,8,18,25,28,6,22,78,32,65,45};
  8.         int index;
  9.  
  10.         PrintArray(arr);
  11.         index = GetMaxOFArray(arr, 10);
  12.         Console.WriteLine(arr[index]);
  13.         SortArrayDescending(arr);
  14.         PrintArray(arr);
  15.         SortArrayAscending(arr);
  16.         PrintArray(arr);
  17.     }
  18.  
  19.     private static void PrintArray(int[] arr)
  20.     {
  21.         Console.Write(arr[0]);
  22.         for (int i = 1; i < arr.Length; i++)
  23.         {
  24.             Console.Write(",{0}", arr[i]);
  25.         }
  26.         Console.WriteLine();
  27.     }
  28.  
  29.     private static void SortArrayDescending(int[] arr)
  30.     {
  31.         int index;
  32.         int buffer;
  33.         for (int i = 0; i < arr.Length; i++)
  34.         {
  35.             index = GetMaxOFArray(arr, i);
  36.             if (index != i)
  37.             {
  38.                 buffer = arr[i];
  39.                 arr[i] = arr[index];
  40.                 arr[index] = buffer;
  41.             }
  42.         }
  43.     }
  44.  
  45.     private static void SortArrayAscending(int[] arr)
  46.     {
  47.         SortArrayDescending(arr);
  48.         Array.Reverse(arr);
  49.     }
  50.  
  51.  
  52.     private static int GetMaxOFArray(int[] arr, int p)
  53.     {
  54.         int index = p;
  55.         for (int i = p+1; i < arr.Length; i++)
  56.         {
  57.             if (ISSecondBigger(arr[index], arr[i]))
  58.             {
  59.                 index = i;
  60.             }
  61.         }
  62.         return index;
  63.     }
  64.     static bool ISSecondBigger(int FirstNumber, int SecondNumber)
  65.     {
  66.         if (FirstNumber < SecondNumber)
  67.         {
  68.             return true;
  69.         }
  70.         else
  71.         {
  72.             return false;
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement