Advertisement
sylviapsh

Sort inn Array with Max Element Index

Jan 22nd, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2.  
  3. class MaxElemInSubArrayAndSortWithIt
  4. {
  5.   //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.
  6.  
  7.   static int GetMaxElementIndex(int[] array, int startIndex, int endIndex)//Returns the max element index in an interval of array indexes [startIndex,endIndex]
  8.   {
  9.     int maxElemIndex = startIndex;//Make the maxElemIndex our start index
  10.     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
  11.     {
  12.       if (array[maxElemIndex] < array[i])//if we find a bigger element
  13.       {
  14.         maxElemIndex = i;//take its index as maximal one
  15.       }
  16.     }
  17.     return maxElemIndex;
  18.   }
  19.  
  20.   static int[] SortAscending(int[] array)//Sorts the elements of the array in ascending order
  21.   {
  22.     int startIndex = 0, //Start index is the start of the array
  23.         endIndex = array.Length - 1,//End index is the end of the array
  24.         currentMaxIndex = 0;//Current max index is set to 0
  25.  
  26.     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
  27.     {
  28.       currentMaxIndex = GetMaxElementIndex(array,startIndex,endIndex); //Returns the max element index for the current interval [startIndex, endIndex]
  29.       int swapBuffer = array[currentMaxIndex]; //Swaps the max element with the last element of the current interval [startIndex, endIndex]
  30.       array[currentMaxIndex] = array[endIndex];
  31.       array[endIndex] = swapBuffer;
  32.     }
  33.     return array;
  34.   }
  35.  
  36.   static int[] SortDescending(int[] array)
  37.   {
  38.     int startIndex = 0,//Start index is the start of the array
  39.         endIndex = array.Length - 1,//End index is the end of the array
  40.         currentMaxIndex = 0;//Current max index is set to 0
  41.     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
  42.     {
  43.       currentMaxIndex = GetMaxElementIndex(array, startIndex, endIndex);//Returns the max element index for the current interval [startIndex, endIndex]
  44.       int swapBuffer = array[currentMaxIndex];//Swaps the max element with the first element of the current interval [startIndex, endIndex]
  45.       array[currentMaxIndex] = array[startIndex];
  46.       array[startIndex] = swapBuffer;
  47.     }
  48.     return array;
  49.   }
  50.  
  51.   static void PrintArray(int[] array)
  52.   {
  53.     for (int i = 0; i < array.Length; i++)
  54.     {
  55.       Console.Write("{0} ", array[i]);
  56.     }
  57.     Console.WriteLine();
  58.   }
  59.  
  60.   static void Main()
  61.   {
  62.     int[] numArray = { 1, 4, 7, 89, 0, 3, 5, 7, -1 };
  63.     int startIndex = 2;
  64.     int endIndex = numArray.Length-1;
  65.  
  66.     Console.WriteLine(GetMaxElementIndex(numArray, startIndex, endIndex)); //Find the index of the maximal element
  67.     PrintArray(SortAscending(numArray)); //Prints the sorted in ascending order numArray
  68.     PrintArray(SortDescending(numArray));//Prints the sorted in descending order numArray
  69.   }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement