How to find the Kth smallest number in an unsorted array with C#

1 Answer

0 votes
using System;

class KthSmallestFinder
{
    public static int FindKthSmallestNumber(int[] arr, int k) {
        Array.Sort(arr);
        
        return arr[k - 1];
    }

    static void Main()
    {
        int[] arr = { 42, 90, 21, 30, 37, 81, 45 };
        int k = 3;

        int result = FindKthSmallestNumber(arr, k);
        
        Console.WriteLine(result); 
    }
}



/*
run:

37

*/

 



answered 1 day ago by avibootz
...