Advertisement
rayhanf

bubble

Jun 9th, 2023
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.68 KB | Source Code | 0 0
  1. program bubbleSort;
  2. uses crt;
  3.  
  4. var
  5.     arr: array[1..10] of integer;
  6.     i, j, 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 bubble sort
  21.     for i := 1 to 9 do begin
  22.         for j := 1 to 10-i do begin
  23.             if (arr[j] > arr[j + 1]) then
  24.             begin
  25.                 temp := arr[j];
  26.                 arr[j] := arr[j + 1];
  27.                 arr[j + 1] := temp;
  28.             end;
  29.         end;
  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