# Recca Chao 的 gitHub page

## 範例程式碼

Binary Search 用 Kotlin 程式語言的寫法為

``````fun search(nums: IntArray, target: Int): Int {
var left = 0
var right = nums.size - 1
while (left <= right) {
val pivot = left + (right - left) / 2
when {
nums[pivot] == target -> return pivot
nums[pivot] > target -> right = pivot - 1
nums[pivot] < target -> left = pivot + 1
}
}
return -1
}
``````

## 計算時間複雜度

``````var left = 0
var right = nums.size - 1
``````

``````return -1
``````

``````val pivot = left + (right - left) / 2
``````

``````when {
nums[pivot] == target -> return pivot
nums[pivot] > target -> right = pivot - 1
nums[pivot] < target -> left = pivot + 1
}
``````

``````while (left <= right) {
}
``````

### while 迴圈次數

``````val pivot = left + (right - left) / 2
when {
nums[pivot] == target -> return pivot
nums[pivot] > target -> right = pivot - 1
nums[pivot] < target -> left = pivot + 1
}
``````

## 計算 Big-O-notation

M = 2 × C_2 + 1

M × log2x = 2 × C_2 × log2x + log2x

M × log2x ＞ C_1 + C_2 × log2x