Advertisement
rayhanf

sort_by_asc_desc

Jun 9th, 2023
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.66 KB | Source Code | 0 0
  1. // Algoritma Insertion Sort (Ascending)
  2. procedure insertionSortAsc(var arr: array of integer; n: integer);
  3. var i, j, key: integer;
  4. begin
  5.     for i := 1 to n-1 do
  6.     begin
  7.         key := arr[i];
  8.         j := i - 1;
  9.  
  10.         while (j >= 0) and (arr[j] > key) do
  11.         begin
  12.             arr[j+1] := arr[j];
  13.             j := j - 1;
  14.         end;
  15.         arr[j+1] := key;
  16.     end;
  17. end;
  18.  
  19. // Algoritma Insertion Sort (Descending)
  20. procedure insertionSortDesc(var arr: array of integer; n: integer);
  21. var i, j, key: integer;
  22. begin
  23.     for i := 1 to n-1 do
  24.     begin
  25.         key := arr[i];
  26.         j := i - 1;
  27.  
  28.         while (j >= 0) and (arr[j] < key) do
  29.         begin
  30.             arr[j+1] := arr[j];
  31.             j := j - 1;
  32.         end;
  33.         arr[j+1] := key;
  34.     end;
  35. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement