---
# System prepended metadata

title: 演算法
tags: [APCS檢定營隊]

---

###### tags: `APCS檢定營隊`
# 演算法
## Liner search
```python=
A = [1,2,3,4,5,6,7,8,9,10]
index=-1
for i in range(len(A)):
    if A[i]==target:
        print('find')
        index=i
        break
print(index)        
```
## binary serach
```python=
a = [1,2,3,4,5,6,7,8,9]
a.sort()
left = 0
right = len(a)-1
target = 11
index = -1
while True:
    mid = (right+left)//2
    if right<left:
        break
    if target==a[mid]:
        print('find')
        index = mid
        break
    if target>a[mid]:
        left = mid+1
    if target<=a[mid]:
        right = mid-1
print(index)
```
## select sort
```python=
def find_min_index(A):
    guess = 0
    for i in range(1,len(A)):
        if A[guess]>A[i]:
            #print('update')
            guess=i
    return guess
def slect_sort(A):
    SORT = []
    for i in range(len(A)):
        #print(A)
        index = find_min_index(A)
        SORT.append(A[index])
        A.pop(index)
    return SORT
A = [21,34,561,1,6735,2314,345,1]
print(slect_sort(A))
A = [21,34,561,1,6735,2314,345,1]
print(sorted(A))
```