Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program SortRecord;
- uses crt;
- type
- TData = record
- id: Integer;
- name: string;
- score: Integer;
- end;
- TArray = array of TData;
- var
- arr: TArray;
- n, i, j, min_idx: Integer;
- temp: TData;
- begin
- clrscr;
- // input jumlah data
- write('Masukkan jumlah data: ');
- readln(n);
- // alokasi memori untuk array
- SetLength(arr, n);
- // input data
- for i := 0 to n-1 do
- begin
- writeln('Data ke-', i+1);
- write('ID: ');
- readln(arr[i].id);
- write('Nama: ');
- readln(arr[i].name);
- write('Nilai: ');
- readln(arr[i].score);
- end;
- // lakukan selection sort pada data berdasarkan ID
- for i := 0 to n-2 do
- begin
- min_idx := i;
- for j := i+1 to n-1 do
- begin
- if arr[j].id < arr[min_idx].id then
- min_idx := j;
- end;
- if min_idx <> i then
- begin
- temp := arr[i];
- arr[i] := arr[min_idx];
- arr[min_idx] := temp;
- end;
- end;
- // tampilkan data setelah diurutkan
- writeln('Data setelah diurutkan berdasarkan ID:');
- for i := 0 to n-1 do
- begin
- writeln('ID: ', arr[i].id, ', Nama: ', arr[i].name, ', Nilai: ', arr[i].score);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement