Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to find the Kth smallest number in an unsorted array with VB.NET

2 Answers

0 votes
Imports System
 
Module Program
 
    Sub Main()
        Dim numbers() As Integer = {42, 90, 21, 30, 37, 81, 45}
        Dim k As Integer = 3
 
        ' Find the Kth smallest number by sorting the array
        Dim result As Integer = FindKthSmallestNumber(numbers, k)
 
        Console.WriteLine(result)
    End Sub
 
 
    ' Returns the Kth smallest number by sorting the array.
    ' This approach is simple, clear, and uses built‑in functionality.
    Function FindKthSmallestNumber(arr() As Integer, k As Integer) As Integer
        ' Work on a copy to avoid modifying the original array
        Dim data() As Integer = CType(arr.Clone(), Integer())
 
        ' Sort the array in ascending order
        Array.Sort(data)
 
        ' K is 1‑based, arrays are 0‑based
        Return data(k - 1)
    End Function
 
End Module
 
 
' run:
'
' 37
'

 



answered Nov 11, 2025 by avibootz
edited 3 days ago by avibootz
0 votes
Imports System
 
Module Program
 
    ' Entry point of the program
    Sub Main()
        ' Example data
        Dim numbers() As Integer = {42, 90, 50, 30, 37, 21, 83, 45}
        Dim k As Integer = 3
 
        ' Find the Kth smallest value using a dedicated function
        Dim result As Integer = FindKthSmallest(numbers, k)
 
        Console.WriteLine($"The {k}rd smallest number is: {result}")
    End Sub
 
 
    ' Finds the Kth smallest element using the Quickselect algorithm.
    ' This approach avoids fully sorting the array, improving efficiency.
    Function FindKthSmallest(values() As Integer, k As Integer) As Integer
        ' Quickselect works in-place, so we operate on a copy to avoid mutating the original array.
        Dim data() As Integer = CType(values.Clone(), Integer())
 
        ' Convert K to zero-based index
        Dim targetIndex As Integer = k - 1
 
        Return QuickSelect(data, 0, data.Length - 1, targetIndex)
    End Function
 
 
    ' Quickselect recursively partitions the array until the pivot lands on the desired index.
    Function QuickSelect(arr() As Integer, left As Integer, right As Integer, targetIndex As Integer) As Integer
        While True
            ' Partition the array and get the pivot's final position
            Dim pivotIndex As Integer = Partition(arr, left, right)
 
            If pivotIndex = targetIndex Then
                ' Found the exact position of the Kth smallest element
                Return arr(pivotIndex)
            ElseIf targetIndex < pivotIndex Then
                ' Search the left partition
                right = pivotIndex - 1
            Else
                ' Search the right partition
                left = pivotIndex + 1
            End If
        End While
         
        return -1
    End Function
 
 
    ' Rearranges elements so that:
    ' - Items less than the pivot are moved to the left
    ' - Items greater than the pivot are moved to the right
    ' Returns the pivot's final index.
    Function Partition(arr() As Integer, left As Integer, right As Integer) As Integer
        Dim pivotValue As Integer = arr(right)
        Dim storeIndex As Integer = left
 
        For i As Integer = left To right - 1
            If arr(i) < pivotValue Then
                Swap(arr, i, storeIndex)
                storeIndex += 1
            End If
        Next
 
        ' Move pivot to its final position
        Swap(arr, storeIndex, right)
        Return storeIndex
    End Function
 
 
    ' Swaps two elements in the array
    Sub Swap(arr() As Integer, i As Integer, j As Integer)
        Dim temp As Integer = arr(i)
        arr(i) = arr(j)
        arr(j) = temp
    End Sub
 
End Module
 
 
' run:
'
' The 3rd smallest number is: 37
'

 



answered 3 days ago by avibootz

Related questions

...