Presentation is loading. Please wait.

Presentation is loading. Please wait.

程式設計--linear search 通訊一甲 B09622048 楊穎穆.

Similar presentations


Presentation on theme: "程式設計--linear search 通訊一甲 B09622048 楊穎穆."— Presentation transcript:

1 程式設計--linear search 通訊一甲 B 楊穎穆

2 目錄 1.目的 2.原理 3.流程圖 4.程式 5.實驗結果顯示 6.參考資料

3 目的 1. 試輸入n個未排序的整數到陣列中 2. 嘗試把未排序的整數到陣列中之數值依照陣列位置與值印出
3. 嘗試用快速排序(Quick Sort)法把陣列中之數值由小而大地排序 4. 嘗試把排序後的整數到陣列中之數值依照陣列位置與值印出 5. 試輸入一個陣列中的值,使用線性搜尋(Linear Search)找到此值在陣列的所在位置並把此位置印出

4 原理 線性搜尋 (Linear search)是最單純的搜尋方式,把資料以陣列來表示,然後用搜尋鍵一一比對,遇到相等的就把資料所在的索引記錄下來,整個陣列比對完後,搜尋就完成了 。 6 9 2 3 7 A[0] A[1] A[2] A[3] A[4]

5 流程圖 NO NO yes yes yes NO main Input x i=0 i<n i=n end 印出 i break
X=A[i] break 印出找不到 NO i++

6 程式 #include <stdio.h> #include <stdlib.h> #define n 8
int Partition(int a[],int p, int r){ int s,j,temp; int x=a[r]; int i=p-1; for(j=p;j<r;j++){ if(a[j]<=x){ i++; if(i !=j){ temp=a[i]; a[i]=a[j]; a[j]=temp; }

7 temp=a[i+1]; a[i+1]=a[j]; a[j]=temp; printf("\n"); for(s=0;s<n;s++) printf(" %d",a[s]); return i+1; } int quicksort(int a[],int p,int r){ int q; if(p<r){ q=Partition(a,p,r); quicksort(a,p,q-1); quicksort(a,q+1,p);

8 int main(void){ int t,A[n],i,x; for(t=0;t<n;t++){ printf(" 請輸入任意數字[%d]:",t); scanf("%d",& A[t]); } quicksort(A,0,n-1); printf("\n由小到大排序後的數字[%d]: ",t); printf("%d \n",A[t]);

9 printf("\n隨意輸入一個數:"); scanf("%d",&x); for(i=0;i<n;i++){ if(x==A[i]){ x=i; i=n; printf("\n搜尋得到的數值=A[%d]",x); } if(i!=n) printf("\n找不到"); system("PAUSE"); return 0;

10 實驗結果顯示

11 實驗結果顯示

12 實驗結果顯示

13 參考資料 主要的參考資料來至”演算法概論”這本書及王志湖老師上課所教授的內容。

14 END


Download ppt "程式設計--linear search 通訊一甲 B09622048 楊穎穆."

Similar presentations


Ads by Google