Elemen yang akan dicari ini disebut key(kunci). Key sebuah data haruslah unik dan berbeda dengan data yang lain. Contoh key adalah NIM.
Terbagi 3:
- Linear Search
membandingkan isi array dari awal sampai key itu sendiri ditemukan.
Algorithm:
1. n : total record of array x.
2. For each x[i], 0 £ i £ n-1, check whether x[i] = key.
3. If x[i] = key, then the searched data is found in index=i. Finished.
4. If x[i] ¹ key, then continue searching until the last data which is i = n-1.
5. If i= n-1 and x[i] ¹ key, it means the data is not exist in the list, and set index = -1. Finished.
- Binary Search
Algorithm:
1. n : total record of array x.
2. left=0, right= n-1.
3. mid =(int) (left + right)/2.
4. If x[mid]=key then index = mid. Finished.
5. If x[mid]<key then left = mid+1.
6. If x[mid]>key then right = mid-1.
7. If left £ right and x[mid] ¹ key, then repeat point 3.
8. If x[mid] ¹ key then index = -1. Finished.
- Interpolation Search
Interpolation ini seperti kita membuka kamus. Jika kita mencari kata berawal huruf B, maka kita mencarinya di bagian akhir dari huruf A sampai bagian awal dari huruf C.
Algorithm:
1.In the interpolation search, we'll split the data according to the following formula:
2.If data[mid] = sought data, data has been found, searching is stopped and return mid.
3.If data[mid]!= sought data, repeat point **
4.**Searching is continued while sought data > data[min] and sought data < data[max].
5.Looking for a mid value by entering into the interpolation formula
6.If data[mid] > sought data, high = mid – 1
7.If data[mid] < sought data, low = mid + 1
8.It will be looped until the requirements point ** are not met then return (-1), data not found
2201770896
skyconnectiva.com
binus.ac.id
Felix Chrissandy


Tidak ada komentar:
Posting Komentar