Coding Sorting dan Searching

program latihan;

uses crt;

type mahasiswa=record

                nim:longint;

                nama:string[20];

                alamat:string[25];

                IP:real;

 end;

const jummhs:integer=3;

var

data:array [1..3] of mahasiswa;

procedure menu;forward;

 

procedure tampil;forward;

 

{======kumpulan prosedur======}

 

procedure inputdata;

var

i:integer;

begin

clrscr;

                for i:=1 to jummhs do

                begin

                                write(‘NIM :’);readln(data[i].nim);

                                write(‘Nama : ‘);readln(data[i].nama);

                                write(‘Alamat : ‘);readln(data[i].alamat);

                write(‘IP : ‘);readln(data[i].ip);

                writeln;

                end;

menu;

end;

 

 

procedure bubblesort;

var

tmp:mahasiswa;

i,j:integer;

begin

                for i:=jummhs downto 2 do

                begin

                                for j:=2 to i do

                                begin

            if data[j].nim < data[j-1].nim then

                                                begin

                                                                tmp:=data[j];

                                                                data[j]:=data[j-1];

                                                                data[j-1]:=tmp;

                                                end;

                                end;

 

                end;

clrscr;

writeln(‘PENGURUTAN DENGAN BUBBLE SORT’);

tampil;

end;

 

procedure selectionsort;

var

tmp:mahasiswa;

i,j,min:integer;

begin

for i:=1 to jummhs-1 do

                begin

                                min:=i;

                                for j:=i+1 to jummhs do

                                begin

           if data[j].nim < data[min].nim then

           data[min].nim:=data[j].nim;

           tmp:=data[min];

           data[min]:=data[i];

           data[i]:=tmp;

                                end;

                end;

clrscr;

writeln(‘PENGURTAN DENGAN SELECTION SORT’);

tampil;

end;

 

procedure insertionsort;

var

i,j:integer;

tmp:mahasiswa;

begin

                for i:=2 to jummhs do

                begin

                                tmp:=data[i];

                                j:=i;

                                while (j>1) and (tmp.nim < data[j-i].nim) do

                                begin

                                                tmp:=data[j];

                                                data[j]:=data[j-1];

                                                j:=j-1;

                                                data[j-1]:=tmp;

        end;

                end;

clrscr;

writeln(‘PENGURTAN DENGAN INSERTION SORT’);

tampil;

end;

 

 

procedure seqsearch;

var

idx:integer;

i:integer;

begin

idx:=1;

for i:=1 to jummhs do

    if data[i].ip > data[idx].ip then idx:=i;

 

clrscr;

writeln(‘NILAI MAKSIMUM by sequensial search’);

gotoxy(3,3);write(‘NIM’);

gotoxy(14,3);Write(‘NAMA’);

gotoxy(29,3);write(‘Alamat’);

gotoxy(44,3);write(‘IP’);

 

gotoxy(3,5);write(data[idx].nim);

gotoxy(14,5);Write(data[idx].nama);

gotoxy(29,5);write(data[idx].alamat);

gotoxy(44,5);write(data[idx].ip:3:2);

readkey;

menu;

end;

 

 

procedure binsearch;

var

atas,bawah,tengah:integer;

cari:longint;

begin

clrscr;

writeln(‘masukkan nim mahasiswa yang akan anda cari’);

readln(cari);

 

atas:=1;

bawah:=jummhs;

tengah:=(atas+bawah)div 2;

while ((atas<bawah) and (data[tengah].nim<>cari)) do

                begin

                                if data[tengah].nim < cari then

                                                atas:= tengah+1

                                else

                                                bawah:=tengah-1;

        tengah:=(atas+bawah)div 2;

        end;

 

                if data[tengah].nim = cari then

                                begin

                        clrscr;

                                                writeln(‘DATA MAHASISWA ADALAH :    by binary search’);

                                                gotoxy(3,3);write(‘NIM’);

                                                gotoxy(14,3);Write(‘NAMA’);

                                                gotoxy(29,3);write(‘Alamat’);

                                                gotoxy(44,3);write(‘IP’);

 

                                                gotoxy(3,5);write(data[tengah].nim);

                                                gotoxy(14,5);Write(data[tengah].nama);

                                                gotoxy(29,5);write(data[tengah].alamat);

                                                gotoxy(44,5);write(data[tengah].ip:3:2);

                                                readkey;

                                                menu;

                                end

                else

                                begin

                                                writeln(‘DATA MAHASISWA TIDAK ADA   by binary search’);

                                                readkey;

                                                menu;

                                end;

end;

 

procedure tampil;

var

i:integer;

begin

gotoxy(3,2);write(‘NIM’);

gotoxy(14,2);Write(‘NAMA’);

gotoxy(29,2);write(‘Alamat’);

gotoxy(44,2);write(‘IP’);

for i:=1 to jummhs do

                begin

                     gotoxy(3,3+i);write(data[i].nim);

                     gotoxy(14,3+i);Write(data[i].nama);

                     gotoxy(29,3+i);write(data[i].alamat);

                     gotoxy(44,3+i);write(data[i].ip:3:2);

                end;

end;

 

 

 

procedure menu;

var

pil:integer;

begin

clrscr;

writeln(‘MENU DATA MAHASISWA’);

writeln(‘[1] MASUKKAN DATA’);

writeln(‘[2] PROGRAM PENGURUTAN DATA’);

writeln(‘[3] PROGRAM PENCARIAN MAHASISWA IPK TERTINGGI’);

writeln(‘[4] PENCARIAN MAHASISWA’);

writeln(‘[5] EXIT’);

writeln;

write(‘Pilihan anda = ‘);

readln(pil);

                case pil of

                                1:inputdata;

                                2:            begin

                                                                bubblesort;

                                                                readkey;

 

                                                                insertionsort;

                                                                readkey;

 

                                                                selectionsort;

                                                                readkey;

 

                                                                menu;

                                                end;

                                3:            begin

                                                                seqsearch;

                                                                menu;

                                                end;

                4:      begin

                                binsearch;

                                menu;

                        end;

                                5:halt;

                end;

end;

 

begin

menu;

end.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s