Advertisement
rayhanf

selection

Jun 9th, 2023
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.72 KB | Source Code | 0 0
  1. program selectionSort;
  2. uses crt;
  3.  
  4. var
  5.     arr: array[1..10] of integer;
  6.     i, j, minIndex, temp: integer;
  7.  
  8. begin
  9.     clrscr;
  10.     //mengisi array dengan data acak
  11.     randomize;
  12.     for i := 1 to 10 do
  13.         arr[i] := random(100);
  14.  
  15.     //menampilkan data sebelum diurutkan
  16.     writeln('Data sebelum diurutkan:');
  17.     for i := 1 to 10 do
  18.         write(arr[i], ' ');
  19.  
  20.     //algoritma selection sort
  21.     for i := 1 to 9 do begin
  22.         minIndex := i;
  23.         for j := i + 1 to 10 do begin
  24.             if (arr[j] < arr[minIndex]) then
  25.             minIndex := j;
  26.         end;
  27.         temp := arr[i];
  28.         arr[i] := arr[minIndex];
  29.         arr[minIndex] := temp;
  30.     end;
  31.  
  32.     //menampilkan data setelah diurutkan
  33.     writeln;
  34.     writeln('Data setelah diurutkan:');
  35.     for i := 1 to 10 do
  36.         write(arr[i], ' ');
  37. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement