Pertemuan XV

advertisement
Lab Algoritma dan Pemrograman 2
Pertemuan XV
Searching
Mahasiswa diharapkan bisa :
1
2
Mengerti algoritma Binary Search.
Membuat program Searching dengan metode Bynary Search.
Materi :
Binary Search
Pencarian data dan posisinya dari sekumpulan data.
Contoh Program untuk Dicoba
Contoh Program XV.1
#include <stdio.h>
#include <conio.h>
int RecursiveSearch(int key, int array[], int low, int high);
int BinarySearch(int key, int array[], int size);
const int MAX = 10;
void main(void)
{
int array[MAX] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int count, found, key;
for (count = 0; count < MAX; count++)
printf("Value: %i [%i]", array[count],count);
printf( "\nEnter a value to search for: ");
scanf(“%i”, &key);
// You can choose which function you want to use by simply removing/adding
// comments between them.
//found = RecursiveSearch(key, array, 0, MAX - 1);
found = BinarySearch(key, array, MAX - 1);
printf( "\nValue %i was found at position %i“, key,found);
getch();
}
/*******************************************************************
* Function: RecursiveSearch
* Purpose: Uses recursion to search array for the desired value.
* Params: key - value to look for, array - array to search for the value,
*
low - staring array index, high - last array index
* Returns:
Position in the array where the value was found or -1 if no match
*
was found.
Lab Algoritma dan Pemrograman 2
Lab Algoritma dan Pemrograman 2
*******************************************************************/
int RecursiveSearch(int key, int array[], int low, int high)
{
int lo = low;
// Stores current lowest index
int hi = high;
// Stores current highest index
int mid = (low + high) / 2;
// Stores the median of low & high
if (!(lo <= hi))
return -1;
if (key == array[mid])
return mid;
// If lo is not <= to high
// Returns negative value
// If there is a match
// Returns the position
if (key < array[mid])
RecursiveSearch(key, array, low, mid - 1); // Highest index is mid - 1
else
// if key > array[mid]
RecursiveSearch(key, array, mid + 1, high); // Lowest index is mid +
1
}
/*******************************************************************
* Function: BinarySearch
* Purpose: Uses loop to search array for the desired value.
* Params: key - value to look for, array - array to search for the value,
*
size of the array (last array index).
* Returns Position in the array where the value was found or -1 if no match
*
was found.
*******************************************************************/
int BinarySearch(int key, int array[], int size)
{
int lo = 0;
int hi = size;
int mid;
while (lo <= hi)
{
mid = (lo + hi) / 2;
if (key == array[mid])
return mid;
if (key < array[mid])
hi = mid - 1;
else
lo = mid + 1;
}
return -1;
}
Lab Algoritma dan Pemrograman 2
Lab Algoritma dan Pemrograman 2
Latihan Soal dan Tugas
1.
Buat program Searching bilangan dari sejumlah bilangan dalam array dengan
data sbb:
12, 7, 5, 10, 22, 3, 8, 11, 15, 6, 17, 1, 2. 13, 14
Carilah masing-masing satu contoh program metode searching Binary Search,
Sequent Search, Recursive Search
2.
Reference
1
2
3
4
5
NIM
Diktat Struktur Data, Hari Soesanto, Ir
Diktat Lab Algoritma dan Struktur Data 2
http://www.google.com
http://www.tf.itb.ac.id/doc/kuliah/metnum/algoritma.html
http://www.planet-source-code.com
: …………………………
Tugas : ………………….………
Nama : ………………………..
……………………………………….
……………………………………..
……………………………………….
Lab Algoritma dan Pemrograman 2
Paraf/Cap Pengajar
(……………………………………..)
Download