How to sort an array of repeated integers with Pascal

2 Answers

0 votes
program SortArrayProgram;

var
  arr: array[1..11] of Integer;
  i, j, temp: Integer;

begin
  // Initialize the array
  arr[1] := 4;
  arr[2] := 2;
  arr[3] := 2;
  arr[4] := 0;
  arr[5] := 8;
  arr[6] := 3;
  arr[7] := 3;
  arr[8] := 5;
  arr[9] := 2;
  arr[10] := 0;
  arr[11] := 1;

  // Bubble Sort
  for i := 1 to 10 do
    for j := 1 to 11 - i do
      if arr[j] > arr[j + 1] then
      begin
        temp := arr[j];
        arr[j] := arr[j + 1];
        arr[j + 1] := temp;
      end;

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


  
  
(*
run:
 
0 0 0 1 2 2 2 3 3 4 5 8 
 
*)

 



answered Jul 5, 2025 by avibootz
0 votes
program SortArrayProgram;

var
  arr: array[1..11] of Integer = (4, 2, 2, 0, 8, 3, 3, 5, 2, 0, 1);
  i, j, temp: Integer;

begin
  // Bubble Sort
  for i := 1 to 10 do
    for j := 1 to 11 - i do
      if arr[j] > arr[j + 1] then
      begin
        temp := arr[j];
        arr[j] := arr[j + 1];
        arr[j + 1] := temp;
      end;

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


  
  
(*
run:
 
0 0 0 1 2 2 2 3 3 4 5 8 
 
*)

 



answered Jul 5, 2025 by avibootz
...