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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,948 questions

51,890 answers

573 users

How to find the Kth smallest number in an unsorted array in Pascal

1 Answer

0 votes
program KthSmallest;

const
  MaxSize = 128;

type
  IntArray = array[1..MaxSize] of Integer;

function FindKthSmallestNumber(var arr: IntArray; size, k: Integer): Integer;
var
  i, j, temp: Integer;
begin
  // Bubble sort
  for i := 1 to size - 1 do
    for j := 1 to size - i do
      if arr[j] > arr[j + 1] then
      begin
        temp := arr[j];
        arr[j] := arr[j + 1];
        arr[j + 1] := temp;
      end;

  FindKthSmallestNumber := arr[k];
end;

var
  arr: IntArray;
  size, k, result: Integer;
begin
  size := 7;
  arr[1] := 42;
  arr[2] := 90;
  arr[3] := 21;
  arr[4] := 30;
  arr[5] := 37;
  arr[6] := 81;
  arr[7] := 45;

  k := 3;
  result := FindKthSmallestNumber(arr, size, k);
  WriteLn(result);
end.



(*
run:

37

*)

 



answered Nov 12, 2025 by avibootz
...