Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MaxElemInSubArrayAndSortWithIt
- {
- //Write a method that return the maximal element in a portion of array of integers starting at given index. Using it write another method that sorts an array in ascending / descending order.
- static int GetMaxElementIndex(int[] array, int startIndex, int endIndex)//Returns the max element index in an interval of array indexes [startIndex,endIndex]
- {
- int maxElemIndex = startIndex;//Make the maxElemIndex our start index
- for (int i = startIndex + 1; i <= endIndex; i++)//Check which is the index of the maxElement in the portion of the array that starts from our startIndex and ends with the array lenght
- {
- if (array[maxElemIndex] < array[i])//if we find a bigger element
- {
- maxElemIndex = i;//take its index as maximal one
- }
- }
- return maxElemIndex;
- }
- static int[] SortAscending(int[] array)//Sorts the elements of the array in ascending order
- {
- int startIndex = 0, //Start index is the start of the array
- endIndex = array.Length - 1,//End index is the end of the array
- currentMaxIndex = 0;//Current max index is set to 0
- for ( startIndex = 0; startIndex <= endIndex; endIndex--)//We'll put each max element at the end and decrease the next interval's end to find the next max element index
- {
- currentMaxIndex = GetMaxElementIndex(array,startIndex,endIndex); //Returns the max element index for the current interval [startIndex, endIndex]
- int swapBuffer = array[currentMaxIndex]; //Swaps the max element with the last element of the current interval [startIndex, endIndex]
- array[currentMaxIndex] = array[endIndex];
- array[endIndex] = swapBuffer;
- }
- return array;
- }
- static int[] SortDescending(int[] array)
- {
- int startIndex = 0,//Start index is the start of the array
- endIndex = array.Length - 1,//End index is the end of the array
- currentMaxIndex = 0;//Current max index is set to 0
- for (startIndex = 0; startIndex <= endIndex; startIndex++)//We'll put each max element at the start and increase the next interval's start to find the next max element index
- {
- currentMaxIndex = GetMaxElementIndex(array, startIndex, endIndex);//Returns the max element index for the current interval [startIndex, endIndex]
- int swapBuffer = array[currentMaxIndex];//Swaps the max element with the first element of the current interval [startIndex, endIndex]
- array[currentMaxIndex] = array[startIndex];
- array[startIndex] = swapBuffer;
- }
- return array;
- }
- static void PrintArray(int[] array)
- {
- for (int i = 0; i < array.Length; i++)
- {
- Console.Write("{0} ", array[i]);
- }
- Console.WriteLine();
- }
- static void Main()
- {
- int[] numArray = { 1, 4, 7, 89, 0, 3, 5, 7, -1 };
- int startIndex = 2;
- int endIndex = numArray.Length-1;
- Console.WriteLine(GetMaxElementIndex(numArray, startIndex, endIndex)); //Find the index of the maximal element
- PrintArray(SortAscending(numArray)); //Prints the sorted in ascending order numArray
- PrintArray(SortDescending(numArray));//Prints the sorted in descending order numArray
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement