Advertisement
Guest User

Слияние

a guest
Dec 11th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.25 KB | None | 0 0
  1. program merge;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8.   System.SysUtils;
  9.  
  10. type
  11.    MyArray = array of Integer;
  12.  
  13. function FindIncreasingSequence(IntArray: MyArray): Integer;
  14. var
  15.    Len, i: Integer;
  16.    IsIncreasing: Boolean;
  17. begin
  18.    Len := Length(INtArray) - 1;
  19.    IsIncreasing := True;
  20.    i := 0;
  21.    while IsIncreasing and (i < Len) do
  22.    begin
  23.       if IntArray[i] > IntArray[i + 1] then
  24.          IsIncreasing := False
  25.       else
  26.          Inc(i);
  27.    end;
  28.    if i = Len then
  29.       FindIncreasingSequence := -1
  30.    else
  31.       FindIncreasingSequence := i;
  32. end;
  33.  
  34. function MergeSort(IntArray: MyArray): MyArray;
  35. var
  36.    ArrayLen, LeftIndex, RightIndex, LeftMax, RightMax, Index: Integer;
  37.    LeftPart, RightPart: MyArray;
  38.   i: Integer;
  39. begin
  40.    Index := FindIncreasingSequence(IntArray);
  41.    if Index <> -1 then
  42.    begin
  43.       ArrayLen := Length(IntArray) - 1;
  44.       LeftPart := Copy(IntArray, 0, Index + 1);
  45.       RightPart := MergeSort(Copy(IntArray, Index + 1, ArrayLen + 1));
  46.       LeftIndex := 0;
  47.       RightIndex := 0;
  48.       LeftMax := Index;
  49.       RightMax := Length(RightPart) - 1;
  50.       for i := 0 to ArrayLen do
  51.       begin
  52.          if (LeftIndex <= LeftMax) and (RightIndex <= RightMax) then
  53.             if LeftPart[LeftIndex] <= RightPart[RightIndex] then
  54.             begin
  55.                IntArray[i] := LeftPart[LeftIndex];
  56.                Inc(LeftIndex);
  57.             end
  58.             else
  59.             begin
  60.                IntArray[i] := RightPart[RightIndex];
  61.                Inc(RightIndex);
  62.             end
  63.          else
  64.             if LeftIndex <= LeftMax then
  65.             begin
  66.                IntArray[i] := LeftPart[LeftIndex];
  67.                Inc(LeftIndex);
  68.             end
  69.             else
  70.                begin
  71.                   IntArray[i] := RightPart[RightIndex];
  72.                   Inc(RightIndex);
  73.                end;
  74.       end;
  75.    end;
  76.    MergeSort := IntArray;
  77. end;
  78.  
  79. var
  80.    IntArray: MyArray;
  81.    i: Integer;
  82.  
  83. begin
  84.    Randomize;
  85.    SetLength(IntArray, 10);
  86.    for i := 0 to 9 do
  87.    begin
  88.       IntArray[i] := Random(20) - 10;
  89.       Write(IntArray[i], ' ');
  90.    end;
  91.    IntArray := MergeSort(IntArray);
  92.    Writeln;
  93.    for i := 0 to 9 do
  94.    begin
  95.       Write(IntArray[i], ' ');
  96.    end;
  97.    Readln;
  98. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement