數(shù)據(jù)結(jié)構(gòu) 實(shí)習(xí)6:查找_第1頁(yè)
數(shù)據(jù)結(jié)構(gòu) 實(shí)習(xí)6:查找_第2頁(yè)
數(shù)據(jù)結(jié)構(gòu) 實(shí)習(xí)6:查找_第3頁(yè)
數(shù)據(jù)結(jié)構(gòu) 實(shí)習(xí)6:查找_第4頁(yè)
數(shù)據(jù)結(jié)構(gòu) 實(shí)習(xí)6:查找_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、實(shí)習(xí)6:查找1、實(shí)驗(yàn)?zāi)康耐ㄟ^(guò)編寫(xiě)和調(diào)用學(xué)過(guò)的三個(gè)查找算法實(shí)現(xiàn)數(shù)據(jù)查找,充分理解各種查找算法的算法思想 及各自的時(shí)間復(fù)雜度、適用性。2、實(shí)驗(yàn)內(nèi)容在排序?qū)嶒?yàn)的基礎(chǔ)上,在順序表類(lèi)SeqList中添加成員函數(shù):不帶監(jiān)視哨的順序查找算法;帶監(jiān)視哨的順序查找算法;二分查找算法;編寫(xiě)主程序,循環(huán)選擇調(diào)用以上3個(gè)查找算法,分別對(duì)鍵入的關(guān)鍵字記錄進(jìn)行 成功和不成功查找。注意:在調(diào)用二分查找算法前必須先對(duì)查找表排序。編譯、運(yùn)行、調(diào)試,觀察排序效果。public class KeyType implements Comparable public int key;public KeyType()public Key

2、Type(int key)this.key = key;public String toString()return key + ”;public int compareTo(KeyType another)int thisVal = this.key;int anotherVal = another.key;return(thisVal anotherVal ? -1 : (thisVal = anotherVal ? 0 : 1);/public class RecordNode public Comparable key;public Object element;public Reco

3、rdNode(Comparable key) this.key = key;public RecordNode(Comparable key,Object element) this.key = key;this.element = element;public Object getKey() return key;/public class SeqList public RecordNode r;public int curlen;public SeqList(int maxSize)this.r = new RecordNodemaxSize;this.curlen = 0;public

4、void clear()curlen = 0;public void insert(int i,RecordNode x)throws Exception if(curlen = r.length)throw new Exception(順序表已滿);if(i curlen)throw new Exception(插入位置不合理);for(int j = curlen; j i;j) rj = rj - 1;ri = x;this.curlen+;public void insertSort()RecordNode temp;int i,j;for(i = 1;i = 0 & pareTo(r

5、j.key) 0;j) r j + 1 = rj;rj + 1 = temp;public void insertSortWithGuard()int i,j;for(i = 1; i this.curlen;i+)r0 = ri;for(j = i- 1;r0.pareTo(rj.key) 0;j) rj + 1 = rj;rj + 1 =r0;public void shellSort(int d)RecordNode temp;int i,j;for(int k = 0;k d.length;k+)int dk = dk;for(i = dk;i = 0 & pareTo(rj.key)

6、 0;j -= dk) rj + dk = rj;rj + dk = temp;public void bubbleSort()RecordNode temp; boolean flag = true;for(int i = 1;i this.curlen & flag;i+) flag = false;for(int j = 0;j 0) temp = rj;rj = rj + 1; rj + 1 =temp; flag = true;public int Partition(int i,int j) RecordNode pivot = ri;while(i j)while(i j & p

7、areTo(rj.key) = 0) j-;if(i j)ri = rj;i+;while(i 0) i+;if(i j) rj = ri; j-;ri = pivot;return i;public void qSort(int low,int high)if(low high)int pivotloc = Partition(low,high); qSort(low,pivotloc - 1);qSort(pivotloc + 1,high);public void quickSort()qSort(0,this.curlen - 1);public int length() return

8、 curlen;public void display() for(int i =0;i this.curlen;i+)System.out.print( + ri.getKey().toString();System.out.println();public int seqSearch(Comparable key)int i = 0,n = length();while(i n & ri.pareTo(key) != 0)i+;if(i 0)return i;elsereturn -1;public int binarySearch(Comparable key)if(length() 0

9、)int low = 0,high = length() - 1;while(low 0) high = mid - 1;elselow = mid + 1;return -1;public void remove(int i) / todo自動(dòng)生成的方法存根/import java.util.Scanner;public class SeqSearch public static void main(String args)throws Exception int口 data = 89,7,46,28,4,53,7,15;int maxSize = 20;SeqList L = new SeqList(maxSize);SeqList LL = new SeqList(maxSize);while(true)for(int i = 0;i data.length;i+)KeyType key = new KeyType(datai);Object element = datai;RecordNode r = new RecordNode(key,eleme

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論