Advertisement
rayhanf

sorting dalam record

Jun 9th, 2023
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.09 KB | Source Code | 0 0
  1. program SortRecord;
  2. uses crt;
  3.  
  4. type
  5.     TData = record
  6.         id: Integer;
  7.         name: string;
  8.         score: Integer;
  9.     end;
  10.     TArray = array of TData;
  11. var
  12.     arr: TArray;
  13.     n, i, j, min_idx: Integer;
  14.     temp: TData;
  15.  
  16. begin
  17.     clrscr;
  18.     // input jumlah data
  19.     write('Masukkan jumlah data: ');
  20.     readln(n);
  21.  
  22.     // alokasi memori untuk array
  23.     SetLength(arr, n);
  24.  
  25.     // input data
  26.     for i := 0 to n-1 do
  27.     begin
  28.         writeln('Data ke-', i+1);
  29.         write('ID: ');
  30.         readln(arr[i].id);
  31.         write('Nama: ');
  32.         readln(arr[i].name);
  33.         write('Nilai: ');
  34.         readln(arr[i].score);
  35.     end;
  36.  
  37.     // lakukan selection sort pada data berdasarkan ID
  38.     for i := 0 to n-2 do
  39.     begin
  40.         min_idx := i;
  41.         for j := i+1 to n-1 do
  42.         begin
  43.             if arr[j].id < arr[min_idx].id then
  44.                 min_idx := j;
  45.         end;
  46.  
  47.         if min_idx <> i then
  48.         begin
  49.             temp := arr[i];
  50.             arr[i] := arr[min_idx];
  51.             arr[min_idx] := temp;
  52.         end;
  53.     end;
  54.  
  55.     // tampilkan data setelah diurutkan
  56.     writeln('Data setelah diurutkan berdasarkan ID:');
  57.     for i := 0 to n-1 do
  58.     begin
  59.         writeln('ID: ', arr[i].id, ', Nama: ', arr[i].name, ', Nilai: ', arr[i].score);
  60.     end;
  61. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement