Bubble Sort ( Struktur Data )


Posting kali ini saya akan membahas mengenai metode pengurutan data menggunakan teknik Bubble Sort secara Ascending dan Descending..berikut ini Program Pascalnya  :


USES CRT;

   CONST
        MAX = 10;
   TYPE
        ARR = ARRAY [1..MAX] OF BYTE;

VAR
   I    : BYTE;
   DATA : ARR;

   PROCEDURE INPUT;
      BEGIN
         CLRSCR;
         WRITELN('MASUKAN 10 DATA');
         WRITELN('===============');

         FOR I := 1 TO MAX DO
         BEGIN
            WRITE('DATA KE-',I ,' = ');READLN(DATA[I]);
         END;

         CLRSCR;
         FOR I := 1 TO MAX DO
         WRITE(DATA[I],' ');

         WRITELN;
         WRITELN('**********************************************');
         WRITELN('DATA YANG TELAH DIURUTKAN : ');
      END;

   PROCEDURE CHANGE (VAR A,B : BYTE); {PROCEDURE UNTUK MENUKAR DATA}
      VAR
         C  : BYTE;
      BEGIN
         C := A;
         A := B;
         B := C;
      END;

   PROCEDURE ASC_BUBLE; {PENGURUTAN SECARA ASCENDING}
      VAR
         P,Q : BYTE;
         FLAG: BOOLEAN;

      BEGIN
         FLAG := FALSE;
         P := 2;
         WHILE ( P < MAX ) AND ( NOT FLAG ) DO
         BEGIN
            FLAG := TRUE;
            FOR Q := MAX DOWNTO P DO
               IF DATA[Q] < DATA [Q-1] THEN
               BEGIN
                  CHANGE( DATA[Q], DATA[Q-1] );
                  FLAG := FALSE;
               END;
               INC(I);
         END;

         WRITE('ASCENDING : ');
      END;

   PROCEDURE DESC_BUBLE; { PENGURUTAN SECARA DESCENDING }
      VAR
         P,Q  : BYTE;
         FLAG : BOOLEAN;

      BEGIN
         FLAG := FALSE;
         P := 2;
         WHILE ( P < MAX ) AND ( NOT FLAG) DO
         BEGIN
            FLAG := TRUE;
            FOR Q:= MAX DOWNTO P DO
               IF DATA[Q] > DATA[Q-1] THEN
               BEGIN
                  CHANGE (DATA[Q], DATA [Q-1]);
                  FLAG := FALSE;
               END;
            INC(I);
         END;
         WRITE('DESCENDING  : ');
      END;

     PROCEDURE OUTPUT;
        BEGIN
           FOR I := 1 TO MAX DO
           WRITE( DATA[I], '  ');
           WRITELN;
        END;



     BEGIN { PROGRAM UTAMA };
        INPUT;
        ASC_BUBLE ; OUTPUT;
        DESC_BUBLE ; OUTPUT;
        READKEY;

     END.


Post a Comment

0 Comments