Saya baru memahami algoritma ilmu komputer. Saya mengerti proses pencarian biner, tetapi saya memiliki sedikit kesalahpahaman dengan efisiensinya.
Dalam ukuran elemen, dibutuhkan, rata-rata, n langkah untuk menemukan elemen tertentu. Mengambil logaritma basis 2 dari kedua sisi menghasilkan log 2 ( s ) = n . Jadi bukankah jumlah rata-rata langkah untuk algoritma pencarian biner adalah log 2 ( s ) ?
Artikel Wikipedia ini tentang algoritma pencarian biner mengatakan bahwa kinerja rata-rata adalah . Kenapa begitu? Mengapa angka ini bukan log 2 ( n ) ?
algorithms
runtime-analysis
binary-search
DrPepper
sumber
sumber
Jawaban:
When you change the base of logarithm the resulting expression differs only by a constant factor which, by definition of Big-O notation, implies that both functions belong to the same class with respect to their asymptotic behavior.
For example
Solog10n and log2n differs by a constant C , and hence both are true:
Another interesting fact with logarithmic functions is that while for constantk>1 , nk is NOT O(n) , but lognk is O(logn) since lognk=klogn which differs from logn by only constant factor k .
sumber
In addition to fade2black's answer (which is completely correct), it's worth noting that the notation "log(n) " is ambiguous. The base isn't actually specified, and the default base changes based on context. In pure mathematics, the base is almost always assumed to be e (unless specified), while in certain engineering contexts it might be 10. In computer science, base 2 is so ubiquitous that log is frequently assumed to be base 2. That wikipedia article never says anything about the base.
But, as has already been shown, in this case it doesn't end up mattering.
sumber