Advertisement
green1ant

mynaturalsort

Dec 11th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.69 KB | None | 0 0
  1. program natural;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   System.SysUtils;
  7.  
  8. type
  9.    TArray = array of Integer;
  10.  
  11. procedure Print(A: TArray);
  12. var
  13.    LastIndex, N, i: Integer;
  14. begin
  15.    LastIndex := High(A);
  16.    for i := 0 to LastIndex do
  17.       Write(A[i], ' ');
  18.    Writeln;
  19. end;
  20.  
  21. function MergeTwoIntoOne(First, Second: TArray): TArray;
  22. var
  23.    i, j, FirstLen, SecondLen, TotalIndex, FirstIndex, SecondIndex: Integer;
  24.    Total: TArray;
  25.  
  26. begin
  27.    FirstLen := Length(First);
  28.    SecondLen := Length(Second);
  29.  
  30.    FirstIndex := 0;
  31.    SecondIndex := 0;
  32.    TotalIndex := 0;
  33.  
  34.    SetLength(Total, FirstLen + SecondLen);
  35.  
  36.    while (FirstIndex < FirstLen) and (SecondIndex < SecondLen) do
  37.    begin
  38.       if First[FirstIndex] < Second[SecondIndex] then
  39.       begin
  40.          Total[TotalIndex] := First[FirstIndex];
  41.          Inc(FirstIndex);
  42.       end
  43.       else
  44.       begin
  45.          Total[TotalIndex] := Second[SecondIndex];
  46.          Inc(SecondIndex);
  47.       end;
  48.       Inc(TotalIndex);
  49.    end;
  50.  
  51.    while FirstIndex < FirstLen do
  52.    begin
  53.       Total[TotalIndex] := First[FirstIndex];
  54.       Inc(FirstIndex);
  55.       Inc(TotalIndex);
  56.    end;
  57.  
  58.    while SecondIndex < SecondLen do
  59.    begin
  60.       Total[TotalIndex] := Second[SecondIndex];
  61.       Inc(SecondIndex);
  62.       Inc(TotalIndex);
  63.    end;
  64.  
  65.    MergeTwoIntoOne := Total;
  66. end;
  67.  
  68. function FindSeparator(A: TArray; From: Integer): Integer;
  69. var
  70.    i, Index, Len: Integer;
  71.    ShouldEnd: Boolean;
  72. begin
  73.    Index := From;
  74.    Len := Length(A);
  75.    ShouldEnd := False;
  76.    while (Index < Len) and not ShouldEnd do
  77.       if A[Index] >= A[Index - 1] then
  78.          Inc(Index)
  79.       else
  80.          ShouldEnd := True;
  81.  
  82.    FindSeparator := Index;
  83. end;
  84.  
  85. procedure MergeSort(Initial: TArray);
  86. var
  87.    i, j, InitialLen, CurIndex, FirstSeparator, SecondSeparator: Integer;
  88.    First, Second, Total: TArray;
  89. begin
  90.    InitialLen := Length(Initial);
  91.    repeat
  92.       FirstSeparator := FindSeparator(Initial, 1);
  93.       SecondSeparator := FindSeparator(Initial, FirstSeparator + 1);
  94.  
  95.       First := Copy(Initial, 0, FirstSeparator);
  96.       Second := Copy(Initial, FirstSeparator, SecondSeparator - FirstSeparator);
  97.  
  98.       Total := MergeTwoIntoOne(First, Second);
  99.  
  100.       for i := 0 to Length(Total) - 1 do
  101.          Initial[i] := Total[i];
  102.  
  103.    until SecondSeparator = InitialLen;
  104. end;
  105.  
  106. var
  107.    N, i: Integer;
  108.    A, Res, d, c: TArray;
  109. begin
  110.    Writeln('Enter N:');
  111.    Readln(N);
  112.    SetLength(A, N);
  113.  
  114.    Randomize;
  115.    for i := 0 to N - 1 do
  116.    begin
  117.       //Write(i, 'th > ');
  118.       //Readln(A[i]);
  119.       A[i] := Random(15);
  120.    end;
  121.  
  122.    Writeln('Unsorted array:');
  123.    Print(A);
  124.  
  125.    MergeSort(A);
  126.    Writeln('Sorted array:');
  127.    Print(A);
  128.    Readln;
  129. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement