Professional Documents
Culture Documents
1 Ny Fs EGt 47 F1 P94 XL JGXW Ya BMG LWW 5 H01
1 Ny Fs EGt 47 F1 P94 XL JGXW Ya BMG LWW 5 H01
⚪ This algorithm searches from left to right and checks whether seeking
element are in the list
⚪ Search in sorted list
⚪ Search in unsorted list
⚪ It is applicable to array or linked list
Searching
Sequential Search
1. Set j = 1
2. if (x = aj) algorithm terminate successfully
3. Increase j by 1
4. If (j≤n) back to 2, otherwise algorithm terminate
unsuccessfully
Searching
Reordering a List for
Maximum Efficiency
⚪ Move-to-front-method
● Efficient only for linked list
● the record is removed from its current
position and placed at the head of the list
⚪ Transposition method
successfully retrieved record is interchange with record that
immediately precedes it
Searching
Argument key is possible:
•In almost end position
SEARCH IN ORDERED
TABLE
Searching
Index Sequential Search
Searching
Binary Search
⚪ The algorithm
lo=0; hi=n-1
while (lo ≤ hi) {
mid=(lo+hi)/2;
if (key==k(mid)) return mid;
if (key<k(mid)) hi=mid-1;
else lo=mid+1;
}
return -1;
Searching
Binary Search (example)
n=13; lo=0; hi=13-1=12; mid=(0+12)/2; key=512;
exampl 61 87 154 170 275 426 503 512 612 653 765 897 908
e:
lo hi
amid<key 🡪lo=mid+1=7; mid=(7+12)/2=9;
61 87 154 170 275 426 503 512 612 653 765 897 908
lo hi
amid=key 🡪 terminate
Searching
Recursive Binary Search
⚪ The algorithm
binSearch(lo,hi);
//lo=0; hi=n-1
while (lo ≤ hi) {
mid=(lo+hi)/2;
if (key==k(mid)) return mid;
if (key<k(mid)) binSearch(lo, mid-1);
else binSearch(mid+1,hi);
}
return -1;
Searching
⚪ Buat aplikasi untuk
● Sequential Search (array/list)
● Move to front(array/list)
● Transposition (arrray/list)
● Index sequential in array
● Binary Search in array
● Recursive Binary Search in array
⚪ Untuk masing2 algoritma, hitung jumlah pembandingan dan
penugasan
Searching