Value type Sorting and Searching Arrays in C#:

Value type Sorting and Searching Arrays in C#:

Sorting is most commonly used Array operation. Array supports a rich complement of sorting methods. We can sort entire array with the help of Sort() method,and one more important thing here that we can efficiently search after performing sorting with the help of BinarySearch().

 

Example:

 

//Sorting and searching

 

Class TestSort

{

   Public static void main()

      {

           //Sorting and searching

 

           int[] numbers ={ 3, 5, 4, 78, 2, -4, 9, 7, 0, 3, 45 };

            // Showing original array

            foreach (int i in numbers)

            {

                Console.Write(i + ",");

            }

            //now we use sort methods

            Array.Sort(numbers);

            //Showing array in sorting order

            foreach (int i in numbers)

            {

                Console.Write(i + ",");

            }

            //Now we can apply binarysearch for element 9

            int findIndex = Array.BinarySearch(numbers, 9);

            Console.WriteLine("index of 9 is" + findIndex);

   }

}