Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class FindBiggestElementInArray
- {
- static void PrintSortedArray(int[] array)
- {
- foreach (var item in array)
- {
- Console.Write(item + " ");
- }
- }
- static int FindBiggestElement(int[] array, int start)
- {
- int indexMax = 0;
- int max = Int32.MinValue;
- for (int i = start; i < array.Length; i++)
- {
- if (array[i] > max)
- {
- max = array[i];
- indexMax = i;
- }
- }
- return indexMax;
- }
- static int[] ChangeElements(int[] array, int firstElement, int secElement)
- {
- int tempElement = array[firstElement];
- array[firstElement] = array[secElement];
- array[secElement] = tempElement;
- return array;
- }
- static int[] SortDescending(int[] array)
- {
- for (int i = 0; i < array.Length; i++)
- {
- ChangeElements(array, FindBiggestElement(array, i), i);
- }
- return array;
- }
- static int[] SortAscending(int[] array)
- {
- SortDescending(array);
- for (int i = 0; i < (array.Length/2); i++)
- {
- ChangeElements(array, array.Length - i - 1, i);
- }
- return array;
- }
- static void Main()
- {
- Console.Write("Enter array lenght: ");
- int arrLenght = int.Parse(Console.ReadLine());
- int[] array = new int[arrLenght];
- for (int i = 0; i < arrLenght; i++)
- {
- array[i] = int.Parse(Console.ReadLine());
- }
- Console.WriteLine("Enter start index");
- int start = int.Parse(Console.ReadLine());
- Console.WriteLine("Biggest number is: {0}", array[FindBiggestElement(array, start)]);
- Console.WriteLine("Press 1 for ascending sort\nPress 0 for descending sort:");
- int typeSort = int.Parse(Console.ReadLine());
- if (typeSort == 1)
- {
- PrintSortedArray(SortAscending(array));
- }
- else
- {
- PrintSortedArray(SortDescending(array));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement