Advertisement
strCarne

Untitled

Apr 17th, 2023 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.10 KB | None | 0 0
  1. program main;
  2.  
  3. uses math;
  4.  
  5. type
  6.     TDynamic = array of Integer;
  7.  
  8. procedure Output(Const arr: TDynamic);
  9. var
  10.     i: Integer;
  11. begin
  12.     for i := Low(arr) to High(arr) do
  13.         write(arr[i], ' ');
  14.     writeln;
  15. end;
  16.  
  17. procedure Heapify(Var arr: TDynamic; Const index, heapLen: Integer);
  18. var
  19.     left, right, tmp: Integer;
  20. begin
  21.     left := 2 * index + 1;
  22.     right := 2 * index + 2;
  23.  
  24.     if (right < heapLen) and (arr[right] > arr[index]) then
  25.     begin
  26.         tmp := arr[right];
  27.         arr[right] := arr[index];
  28.         arr[index] := tmp;
  29.         Heapify(arr, right, heapLen);
  30.     end;
  31.  
  32.     if (left < heapLen) and (arr[left] > arr[index]) then
  33.     begin
  34.         tmp := arr[left];
  35.         arr[left] := arr[index];
  36.         arr[index] := tmp;
  37.         Heapify(arr, left, heapLen);
  38.     end;
  39. end;
  40.  
  41. procedure BuildMaxHeap(Var arr: TDynamic);
  42. var
  43.     i: Integer;
  44. begin
  45.     for i := length(arr) - 1 downto 0 do
  46.         Heapify(arr, i, length(arr));
  47. end;
  48.  
  49. procedure InitHeap(Var arr: TDynamic; Const size: Integer);
  50. var
  51.     i: Integer;
  52. begin
  53.     SetLength(arr, size);
  54.     for i := Low(arr) to High(arr) do
  55.     begin
  56.         write(i, ' elem = ');
  57.         readln(arr[i]);
  58.     end;
  59.     BuildMaxHeap(arr)
  60. end;
  61.  
  62. function PopElem(Var arr: TDynamic; heapLen: Integer): Integer;
  63. begin
  64.     PopElem := arr[0];
  65.     arr[0] := arr[heapLen - 1];
  66.     heapLen := heapLen - 1;
  67.     Heapify(arr, 0, heapLen);
  68.     SetLength(arr, heapLen);
  69. end;
  70.  
  71. procedure InsertElem(Var arr: TDynamic; Const elem: Integer);
  72. var
  73.     InsInd: Integer;
  74.     parent: Integer;
  75.     tmp: Integer;
  76.     StopFlag: Boolean = False;
  77. begin
  78.     SetLength(arr, length(arr) + 1);
  79.     InsInd := length(arr) - 1;
  80.     arr[InsInd] := elem;
  81.     while (InsInd <> 0) or (not StopFlag) do
  82.     begin
  83.         parent := floor(InsInd / 2);
  84.         if arr[parent] < arr[InsInd] then
  85.         begin
  86.             tmp := arr[parent];
  87.             arr[parent] := arr[InsInd];
  88.             arr[InsInd] := tmp;
  89.             InsInd := parent;
  90.         end
  91.         else
  92.             StopFlag := True;
  93.     end;
  94. end;
  95.  
  96. {
  97.     M A I N ! ! !
  98. }
  99.  
  100. var
  101.     N: Integer;
  102.     heap: TDynamic;
  103.     tmp: Integer;
  104.  
  105. begin
  106.     write('Enter N: ');
  107.     readln(N);
  108.     InitHeap(heap, N);
  109.     Output(heap);
  110.     tmp := PopElem(heap, length(heap));
  111.     writeln(tmp);
  112.     Output(heap);
  113.     tmp := 10;
  114.     InsertElem(heap, tmp);
  115.     Output(heap);
  116. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement