# wk13_1130_演算法_B1003205醫放三謝彤
## [inclass practice]
{範例}\
由小排到大排序 <bubble>\
追蹤泡沫排序法的過程 <bubble_debuge>
```python
def switch(i):#這個swith是function,可以自己訂名字
datas[i],datas[i+1]=datas[i+1],datas[i]
datas=[3,5,2,1]
print("排序前",datas)
switch(2) #記得,順序是0123 (從0開始算)
print("排序後",datas)
```
排序前 [3, 5, 2, 1]
排序後 [3, 5, 1, 2]
```python
def switch(i):
datas[i],datas[i+1]=datas[i+1],datas[i]
datas=[3,5,2,1]
print("排序前",datas)
n=len(datas)
for i in range(0,n):
for j in range(0,n-1):
if datas[j]>datas[j+1]:
print(datas[j],datas[j+1],end="")
switch(j)
print(datas)
print("排序後",datas)
```
排序前 [3, 5, 2, 1]
5 25 1[3, 2, 1, 5]
3 23 1[2, 1, 3, 5]
2 1[1, 2, 3, 5]
[1, 2, 3, 5]
排序後 [1, 2, 3, 5]
### 隨機跳轉WIKI文章
```python
! pip install wikipedia beautifulsoup4
```
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: wikipedia in c:\users\user\appdata\roaming\python\python310\site-packages (1.4.0)
Requirement already satisfied: beautifulsoup4 in c:\programdata\anaconda3\lib\site-packages (4.11.1)
Requirement already satisfied: requests<3.0.0,>=2.0.0 in c:\programdata\anaconda3\lib\site-packages (from wikipedia) (2.28.1)
Requirement already satisfied: soupsieve>1.2 in c:\programdata\anaconda3\lib\site-packages (from beautifulsoup4) (2.3.2.post1)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\programdata\anaconda3\lib\site-packages (from requests<3.0.0,>=2.0.0->wikipedia) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\programdata\anaconda3\lib\site-packages (from requests<3.0.0,>=2.0.0->wikipedia) (1.26.14)
Requirement already satisfied: idna<4,>=2.5 in c:\programdata\anaconda3\lib\site-packages (from requests<3.0.0,>=2.0.0->wikipedia) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\anaconda3\lib\site-packages (from requests<3.0.0,>=2.0.0->wikipedia) (2023.5.7)
```python
import requests
from bs4 import BeautifulSoup
import webbrowser
while True:
url=requests.get("https://zh.wikipedia.org/wiki/Special:Random")
soup=BeautifulSoup(url.content,"html.parser")
title=soup.find(class_="firstHeading").text
print(f"{title}\nDo you want to view it?(Y/N)")
ans=input("").lower()
if ans=="y":
url="https://zh.wikipedia.org/wiki/%s" %title
webbrowser.open(url)
break
elif ans=="n":
print("Try again!")
continue
else:
primt("Wrong choice!")
break
```
广州中医药大学
Do you want to view it?(Y/N)
y
## [afterclass practice]
### 綜合演練
1.執行下列程式,下列結果何者正確?\
(A)[3,5,2,1] (B)[5,3,2,1] (C)[1,2,3,5] (D)[3,5,1,2]
Answer:(C)
```python
datas=[3,5,2,1]
n=len(datas)-1
for i in range(0,n):
for j in range(0,n-i):
if(datas[j]>datas[j+1]):
datas[j],datas[j+1]=datas[j+1],datas[j]
print(datas)
```
[1, 2, 3, 5]
2.執行下列程式,下列顯示結果何者正確?\
(A)52 (B)i (C)no (D)3
Answer:(D)
```python
num=[67,12,9,52,91,3]
no=52
for i in range(len(num)):
if (num[i]==no):
break
print(i)
```
3
3.下列那一個排序法,在執行搜尋前必須先將資料排序?\
(A)循序搜尋法 (B)二分搜尋法 (C)泡沫搜尋法 (D)以上皆是
Answer:(B)
4.有10000筆資料時,使用循序搜尋最少需多少次?\
(A)1 (B)10000 (C)15 (D)14
Answer:(A)
5.有10000筆資料時,使用循序搜尋最多需多少次?\
(A)1 (B)10000 (C)15 (D)14
Answer:(B)
6.有10000筆資料時,使用二分搜尋最多需多少次?\
(A)1 (B)10000 (C)15 (D)14
Answer:(D)
7.下列哪一種搜尋方法效率最好?\
(A)二分搜尋 (B)循序搜尋 (C)泡沫搜尋 (D)三者效率相同
Answer:(A)
8.執行下列程式,下列結果何者正確?\
(A)no=256 (B)IsFound=True (C)共比對9次 (D)以上皆非
Answer:(D)
```python
num=[256,731,943,389,142,645,829,945]
name=["林小虎","王中森","邵木淼","李大同","陳子孔","鄭美麗","曾溫柔","錢來多"]
no=100
IsFound=False
for i in range(len(name)): #逐一比對搜尋
if (num[i]==no): #號碼相符
IsFound=True #設旗標為True
break #結束比對
if (IsFound==True):
print("中獎者的姓名為:",name[i])
else:
print("無此中獎號碼!")
print("共比對%d次"%(i+1))
```
無此中獎號碼!
共比對8次
9.同第8題,中獎者的姓名為?\
(A)錢來多 (B)曾溫柔 (C)鄭美麗 (D)無此中獎號碼!
Answer:(D)
10.同第8題,這個程式是?\
(A)二分搜尋 (B)循序搜尋 (C)泡沫搜尋 (D)以上皆非
Answer:(B)
### 名詞解釋
演算法:解決問題的方法,寫成虛擬碼或流程圖,以便理清思緒以及與他人溝通。
虛擬碼:以文字表示程式碼的流程,包含程式開始、結束,輸入、輸出。
流程圖:以圖框加文字的方式表示整個程式流程。橢圓表示開始、結束;平行四邊形表示input;長方形表示process;菱形表示判斷。再以箭頭標示流程順序。
## [self practice]
演算法即解決問題的方法,寫成虛擬碼或流程圖,跟別人溝通
流程圖的標示方法\
橢圓:開始、結束。平行四邊形:input。長方形:PROCESS。菱形:判斷。再以箭頭標示流程順序
泡沫排序:\
先比較相鄰的元素,依小到大排序;做迴圈直到最後一個數不需要比較