How to sort the part of an array in Pascal

1 Answer

0 votes
program PartialSort;

var
  arr: array of Integer;
  i: Integer;

// Procedure to sort a portion of the array from startIndex to endIndex (inclusive)
procedure SortSubArray(var a: array of Integer; startIndex, endIndex: Integer);
var
  i, j, temp: Integer;
begin
  for i := startIndex to endIndex do
    for j := i + 1 to endIndex do
      if a[j] < a[i] then
      begin
        temp := a[i];
        a[i] := a[j];
        a[j] := temp;
      end;
end;

begin
  // Set the length of the dynamic array
  SetLength(arr, 9);

  // Assign values manually
  arr[0] := 15;
  arr[1] := 6;
  arr[2] := 19;
  arr[3] := 8;
  arr[4] := 3;
  arr[5] := 7;
  arr[6] := 9;
  arr[7] := 1;
  arr[8] := 4;

  // Sort elements from index 2 to 6 (inclusive)
  SortSubArray(arr, 2, 6);

  // Print the updated array
  for i := 0 to High(arr) do
    Write(arr[i], ' ');
  WriteLn;
end.

 



answered Aug 12, 2025 by avibootz
edited Aug 12, 2025 by avibootz
...