PRAKTIKUM ALGORITMA DAN STRUKTUR DATA MODUL KE

advertisement
PRAKTIKUM ALGORITMA DAN STRUKTUR DATA
MODUL KE-7
ALGORITMA
PENCARIAN DAN PENGURUTAN
LABORATORIUM PEMROGRAMAN
PROGRAM STUDI TEKNIK INFORMATIKA
FAKULTAS TEKNIK
UNIVERSITAS MUHAMMADIYAH MALANG
2010
I.
TUJUAN
Mahasiswa mampu :
1. Memahami algoritma pencarian (linear search dan binary search)
2. Menerapkan algoritma pencarian kedalam pemrograman
3. Memahami algoritma pengurutan (insertion sort, selection sort, dan bubble sort)
4. Menerapkan algoritma pengurutan kedalam pemrograman
II. ALAT YANG DIGUNAKAN
Peralatan yang digunakan :
1.Perangkat PC yang terinstall Java
2.Editor Java
III. DASAR TEORI
IV. PROSEDUR PELAKSANAAN
Prosedur pelaksanaan praktikum adalah sebagai berikut :
1.Mahasiswa menerima tugas yang diberikan oleh dosen/asisten
2.Mahasiswa mengerjakan tugas yang diberikan
3.Asisten/dosen menilai pekerjaan mahasiswa
V. LATIHAN
ALGORITMA PENCARIAN
1. Linear atau Sequential Search
import java.util.Arrays;
import java.util.Scanner;
import static java.lang.Math.*;
public class SequentialSearch
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int j, k, number, size;
boolean found = false;
System.out.print("Enter the array size: ");
size = sc.nextInt();
int[] intArray = new int[size];
for (j=0; j<size; j++)
intArray[j] = (int) (random()*100);
System.out.println(" " + size + " element int array of random ints between 0 and 99 has been
loaded.");
System.out.println("The random array for verification is:\n\n");
System.out.println(Arrays.toString(intArray));
System.out.println("\nGuess one of the numbers: ");
number = sc.nextInt();
//// linear search algorithm
k=0;
while (!found && k<size)
if (intArray[k]==number)
found=true;
else
k +=1;
System.out.println();
if (found)
System.out.println("Found in position " + k);
else
System.out.println("The number is not found");
/////////////////////////
}
}
2. Binary Search
// orderedArray.java
// demonstrates ordered array class
// to run this program: C>java OrderedApp
////////////////////////////////////////////////////////////////
class OrdArray
{
private long[] a; // ref to array a
private int nElems; // number of data items
//----------------------------------------------------------public OrdArray(int max) // constructor
{
a = new long[max]; // create array
nElems = 0;
}
//----------------------------------------------------------public int size()
{ return nElems; }
//----------------------------------------------------------public int find(long searchKey)
{
int lowerBound = 0;
int upperBound = nElems-1;
int curIn;
while(true)
{
curIn = (lowerBound + upperBound ) / 2;
if(a[curIn]==searchKey)
return curIn; // found it
else if(lowerBound > upperBound)
return nElems; // can’t find it
else // divide range
{
if(a[curIn] < searchKey)
lowerBound = curIn + 1; // it’s in upper half
else
upperBound = curIn - 1; // it’s in lower half
} // end else divide range
} // end while
} // end find()
//----------------------------------------------------------public void insert(long value) // put element into array
{
int j;
for(j=0; j<nElems; j++) // find where it goes
if(a[j] > value) // (linear search)
break;
for(int k=nElems; k>j; k--) // move bigger ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
//----------------------------------------------------------public boolean delete(long value)
{
int j = find(value);
if(j==nElems) // can’t find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move bigger ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//----------------------------------------------------------public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//----------------------------------------------------------} // end class OrdArray
///////////////////////////////////////////////////////////////
class OrderedApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
OrdArray arr; // reference to array
arr = new OrdArray(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display(); // display items
int searchKey = 99; // search for item
System.out.println("Search key " + searchKey);
if( arr.find(searchKey) != arr.size() )
System.out.println("Found " + searchKey);
else
System.out.println("Can’t find " + searchKey);
} // end main()
} // end class OrderedApp
ALGORITMA PENGURUTAN
3. Selection Sort
// selectSort.java
// demonstrates selection sort
// to run this program: C>java SelectSortApp
////////////////////////////////////////////////////////////////
class SelectionSort
{
private long[] a; // ref to array a
private int nElems; // number of data items
//-------------------------------------------------------------public SelectionSort(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//-------------------------------------------------------------public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//-------------------------------------------------------------public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//-------------------------------------------------------------public void selectionSort()
{
int out, in, min;
for(out=0; out<nElems-1; out++) // outer loop
{
min = out; // minimum
for(in=out+1; in<nElems; in++) // inner loop
if(a[in] < a[min] ) // if min greater,
min = in; // we have a new min
swap(out, min); // swap them
} // end for(out)
} // end selectionSort()
//-------------------------------------------------------------private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//-------------------------------------------------------------} // end class ArraySel
////////////////////////////////////////////////////////////////
class SelectSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
SelectionSort arr; // reference to array
arr = new SelectionSort(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display(); // display items
arr.selectionSort(); // selection-sort them
arr.display(); // display them again
} // end main()
} // end class SelectSortApp
4. Insertion Sort
// insertSort.java
// demonstrates insertion sort
// to run this program: C>java InsertSortApp
//-------------------------------------------------------------class InsertionSort
{
private long[] a; // ref to array a
private int nElems; // number of data items
//-------------------------------------------------------------public InsertionSort(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//-------------------------------------------------------------public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//-------------------------------------------------------------public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//-------------------------------------------------------------public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
long temp = a[out]; // remove marked item
in = out; // start shifts at out
while(in>0 && a[in-1] >= temp) // until one is smaller,
{
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
//-------------------------------------------------------------} // end class ArrayIns
////////////////////////////////////////////////////////////////
class InsertSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
InsertionSort arr; // reference to array
arr = new InsertionSort(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display(); // display items
arr.insertionSort(); // insertion-sort them
arr.display(); // display them again
} // end main()
} // end class InsertSortApp
5. Bubble Sort
// bubbleSort.java
// demonstrates bubble sort
// to run this program: C>java BubbleSortApp
////////////////////////////////////////////////////////////////
class ArrayBub
{
private long[] a; // ref to array a
private int nElems; // number of data items
//-------------------------------------------------------------public ArrayBub(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//-------------------------------------------------------------public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//-------------------------------------------------------------public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//-------------------------------------------------------------public void bubbleSort()
{
int out, in;
for(out=nElems-1; out>1; out--) // outer loop (backward)
for(in=0; in<out; in++) // inner loop (forward)
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
//-------------------------------------------------------------private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//-------------------------------------------------------------} // end class ArrayBub
////////////////////////////////////////////////////////////////
class BubbleSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayBub arr; // reference to array
arr = new ArrayBub(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display(); // display items
arr.bubbleSort(); // bubble sort them
arr.display(); // display them again
} // end main()
} // end class BubbleSortApp
VI. TUGAS PRAKTIKUM
1. (1 orang) Modifikasi program pada latihan 1 (sequential search) untuk mendapatkan data
yang dicari beserta jumlah data tersebut muncul pada deret data.
Asumsi :
-
Deret data bukan hasil dari pembangkitan bilangan secara random tetapi berasal dari
masukan oleh user satu persatu.
-
Deret data tidak bersifat unique, dalam arti mampu menampung data yang memiliki
nilai sama.
2. (1 orang) Modifikasi program pada latihan 2 (binary search) sehingga output yang
dihasilkan dapat menampilkan index dari data yang dicari.
3. (1 orang) Modifikasi salah satu program pengurutan diatas (latihan 3,4,5) untuk
menampilkan output berupa data terurut secara descending.
4. (2 orang) Gabungkan ketiga algoritma pengurutan diatas dalam class. Bangkitkan
bilangan secara acak dengan jumlah bilangan yang dibangkitkan berturut-turut adalah :
100, 1000, dan 10000. Lakukan pengurutan secara ascending pada bilangan tsb secara
bersamaan (sekali eksekusi). Kemudian lakukan analisa terhadap waktu dengan
menghitung berapa lama waktu yang dibutuhkan tiap algoritma untuk melakukan
pengurutan (petunjuk : tambahkan fungsi waktu di awal (A) dan di akhir (B) proses
untuk setiap algoritma).
Hint :
Lama proses (detik) = waktu akhir(A) – waktu awal(B)
Dari percobaan tersebut lengkapi data pada table berikut :
Percobaan
1
2
3
4
Algoritma
Selection Sort
Jumlah Bilangan
Waktu (detik)
100
….
Insertion Sort
…..
Bubble Sort
…..
Selection Sort
1000
….
Insertion Sort
…..
Bubble Sort
…..
Selection Sort
10000
….
Insertion Sort
…..
Bubble Sort
…..
Selection Sort
100000
….
Insertion Sort
…..
Bubble Sort
…..
VII. HASIL PRAKTIKUM
[Tuliskan hasil tugas praktikum di sini]
Download