




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)專業(yè):計(jì)算機(jī)科學(xué)與技術(shù)姓名:周兵學(xué)號(hào):0309401221 / 171、 需求分析一、設(shè)計(jì)代碼:1. 直接插入排序:public class InsertSort public static <T extends Comparable<? super T>> void insertSort(T array) for (int i = 1; i <= array.length - 1; i+) int j = i; while (j>=1 && pareTo(arrayj - 1) < 0) T te
2、mp = arrayj; arrayj = arrayj - 1; arrayj - 1 = temp; j-; public static void main(String args) Integer testArray = 23, 25, 12, 42, 35,33,43,57; System.out.println("排序前 :"); for (Integer item : testArray) System.out.print(item); System.out.print(' '); System.out.println(); System.out
3、.println("-"); InsertSort.insertSort(testArray); System.out.println("排序后 :"); for (Integer item : testArray) System.out.print(item); System.out.print(' '); 實(shí)驗(yàn)結(jié)果:2. 折半插入排序:public class BInsertSort public static void bInsertSort(int temp) int length = temp.length; for (int
4、i = 1; i < length; i+) int tempVal = tempi; int low = 0; int high = i - 1; while (low <= high) int middle = (low + high) / 2; if (tempVal < tempmiddle) high = middle - 1; else low = middle + 1; for (int j = i; j > high + 1; j-) tempj = tempj - 1; temphigh + 1 = tempVal; public static voi
5、d main(String args) int a = 5, 1, 76, 2, 4, 84, 36, 22, 62, 90 ; bInsertSort(a); System.out.println("排序后:"); for (int i = 0; i < a.length; i+) System.out.print(ai + " "); 實(shí)驗(yàn)結(jié)果:3. 希爾排序:public class InsertionSort public void shellInertionSort(double sorted, int inc) int sortedLe
6、n= sorted.length; for(int j=inc+1;j<sortedLen;j+ ) if(sortedj<sortedj-inc) sorted0= sortedj;/先保存一下后面的那個(gè) int insertPos=j; for(int k=j-inc;k>=0;k-=inc) if(sortedk>sorted0) sortedk+inc=sortedk; if(k-inc<=0) insertPos = k; else insertPos=k+inc; break; sortedinsertPos=sorted0; public void
7、shellInsertionSort(double sorted) int incs=7,5,3,1; int num= incs.length; int inc=0; for(int j=0;j<num;j+) inc= incsj; shellInertionSort(sorted,inc); public static void main(String args) Random random= new Random(6); int arraysize= 21; double sorted=new doublearraysize; System.out.print("Bef
8、ore Sort:"); for(int j=1;j<arraysize;j+) sortedj= (int)(random.nextDouble()* 100); System.out.print(int)sortedj+" "); System.out.println(); InsertionSort sorter=new InsertionSort(); sorter.shellInsertionSort(sorted); System.out.print("After Sort:"); for(int j=1;j<sorte
9、d.length;j+) System.out.print(int)sortedj+" "); System.out.println();實(shí)驗(yàn)結(jié)果:4. 冒泡排序:public class BubbluSort public static void sortiere(int x) boolean unsortiert = true;int temp;while (unsortiert) unsortiert = false;for (int i = 0; i < x.length - 1; i+)if (xi > xi + 1) temp = xi;xi = x
10、i + 1;xi + 1 = temp;unsortiert = true;public static void main(String args) int liste = 0, 9, 4, 6, 2, 8, 5, 1, 7, 3 ;System.out.println("排序前:");for (int i = 0; i < liste.length; i+)System.out.print(listei + " ");System.out.println();System.out.println("-");System.out
11、.println("排序后:");sortiere(liste);for (int i = 0; i < liste.length; i+)System.out.print(listei + " "); 實(shí)驗(yàn)結(jié)果:5. 快速排序:public class ExchangeSort public void QuickExchangeSortBackTrack(double sorted,int low, int high) if (low < high) int pivot = findPivot(sorted, low, high);Quic
12、kExchangeSortBackTrack(sorted, low, pivot - 1);QuickExchangeSortBackTrack(sorted, pivot + 1, high);public int findPivot(double sorted, int low, int high) sorted0 = sortedlow;while (low < high) while (low < high && sortedhigh >= sorted0)-high;sortedlow = sortedhigh;while (low < hi
13、gh && sortedlow <= sorted0)+low;sortedhigh = sortedlow;sortedlow = sorted0;return low;public static void main(String args) Random random = new Random(6);int arraysize = 10;double sorted = new doublearraysize;System.out.println("排序前:");for (int j = 1; j < arraysize; j+) sorted
14、j = (int) (random.nextDouble() * 100);System.out.print(int) sortedj + " ");System.out.println();System.out.println("-");ExchangeSort sorter = new ExchangeSort();sorter.QuickExchangeSortBackTrack(sorted, 1, arraysize - 1);System.out.println("排序后:");for (int j = 1; j <
15、 sorted.length; j+) System.out.print(int) sortedj + " ");System.out.println();實(shí)驗(yàn)結(jié)果:6. 直接選擇排序:public class SelectionSort public void straitSelectionSort(double sorted) int sortedLen= sorted.length; for(int j=1;j<sortedLen;j+) int jMin= getMinIndex(sorted,j); exchange(sorted,j,jMin); publ
16、ic void exchange(double sorted,int i,int j) int sortedLen= sorted.length; if(i<sortedLen && j<sortedLen && i<j && i>=0 && j>=0) double temp= sortedi; sortedi=sortedj; sortedj=temp; public int getMinIndex(double sorted, int i) int sortedLen= sorted.lengt
17、h; int minJ=1; double min= Double.MAX_VALUE; for(int j=i;j<sortedLen;j+) if(sortedj<min) min= sortedj; minJ= j; return minJ; public static void main(String args) Random random= new Random(6); int arraysize=9; double sorted=new doublearraysize; System.out.println("排序前:"); for(int j=1;
18、j<arraysize;j+) sortedj= (int)(random.nextDouble()* 100); System.out.print(int)sortedj+" "); System.out.println(); System.out.println("-"); SelectionSort sorter=new SelectionSort(); sorter.straitSelectionSort(sorted); System.out.println("排序后:"); for(int j=1;j<sort
19、ed.length;j+) System.out.print(int)sortedj+" "); System.out.println(); 實(shí)驗(yàn)結(jié)果:7. 堆排序:public class SelectionSort public void heapAdjust(double sorted,int start,int end) if(start<end) double temp= sortedstart; for(int j=2*start;j<end;j *=2) if(j+1<end && sortedj-sortedj+1>1
20、0e-6) +j; if(temp<=sortedj) break; sortedstart=sortedj; start=j; sortedstart=temp; public void exchange(double sorted,int i,int j) int sortedLen= sorted.length; if(i<sortedLen && j<sortedLen && i<j && i>=0 && j>=0) double temp= sortedi; sortedi=sorte
21、dj; sortedj=temp; public void heapSelectionSort(double sorted) int sortedLen = sorted.length; for(int i=sortedLen/2;i>0;i-) heapAdjust(sorted,i,sortedLen); for(int i=sortedLen;i>1;-i) exchange(sorted,1,i); heapAdjust(sorted,1,i-1); public static void main(String args) Random random= new Random
22、(6); int arraysize=10; double sorted=new doublearraysize; System.out.println("排序前:"); for(int j=1;j<arraysize;j+) sortedj= (int)(random.nextDouble()* 100); System.out.print(int)sortedj+" "); System.out.println(); System.out.println("-"); SelectionSort sorter=new Sele
23、ctionSort(); sorter.heapSelectionSort(sorted); System.out.println("排序后:"); for(int j=1;j<sorted.length;j+) System.out.print(int)sortedj+" "); System.out.println(); 實(shí)驗(yàn)結(jié)果:8. 歸并排序:public class MergeSort private double bridge;/輔助數(shù)組 public void sort(double obj) if (obj = null) thro
24、w new NullPointerException("The param can not be null!"); bridge = new doubleobj.length; / 初始化中間數(shù)組 mergeSort(obj, 0, obj.length - 1); / 歸并排序 bridge = null; private void mergeSort(double obj, int left, int right) if (left < right) int center = (left + right) / 2; mergeSort(obj, left, cen
25、ter); mergeSort(obj, center + 1, right); merge(obj, left, center, right); private void merge(double obj, int left, int center, int right) int mid = center + 1; int third = left; int tmp = left; while (left <= center && mid <= right) if (objleft-objmid<=10e-6) bridgethird+ = objleft+
26、; else bridgethird+ = objmid+; while (mid <= right) bridgethird+ = objmid+; while (left <= center) bridgethird+ = objleft+; / 將中間數(shù)組的內(nèi)容拷貝回原數(shù)組 copy(obj, tmp, right); private void copy(double obj, int left, int right) while (left <= right) objleft = bridgeleft; left+; public static void main(S
27、tring args) Random random = new Random(6); int arraysize = 10; double sorted = new doublearraysize; System.out.println("排序前:"); for (int j = 0; j < arraysize; j+) sortedj = (int) (random.nextDouble() * 100); System.out.print(int) sortedj + " "); System.out.println(); System.ou
28、t.println("-"); MergeSort sorter = new MergeSort(); sorter.sort(sorted); System.out.println("排序后:"); for (int j = 0; j < sorted.length; j+) System.out.print(int) sortedj + " "); System.out.println(); 實(shí)驗(yàn)結(jié)果:9. 基數(shù)排序:public class RadixSort private int keyNum=-1; private
29、Vector<Vector<Double>> util; public void distribute(double sorted, int nth) if(nth<=keyNum && nth>0) util=new Vector<Vector<Double>>(); for(int j=0;j<10;j+) Vector <Double> temp= new Vector <Double>(); util.add(temp); for(int j=0;j<sorted.lengt
30、h;j+) int index= getNthDigit(sortedj,nth); util.get(index).add(sortedj); public int getNthDigit(double num,int nth) String nn= Integer.toString(int)num); int len= nn.length(); if(len>=nth) return Character.getNumericValue(nn.charAt(len-nth); else return 0; public void collect(double sorted) int k
31、=0; for(int j=0;j<10;j+) int len= util.get(j).size(); if(len>0) for(int i=0;i<len;i+) sortedk+= util.get(j).get(i); util=null; public int getKeyNum(double sorted) double max= Double.MIN_VALUE; for(int j=0;j<sorted.length;j+) if(sortedj>max) max= sortedj; return Integer.toString(int)max).length();
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 新鄉(xiāng)職業(yè)技術(shù)學(xué)院《分子細(xì)胞生物學(xué)專論》2023-2024學(xué)年第二學(xué)期期末試卷
- 浙江橫店影視職業(yè)學(xué)院《流體輸配管網(wǎng)課程設(shè)計(jì)》2023-2024學(xué)年第一學(xué)期期末試卷
- 浙江省慈溪市六校2024-2025學(xué)年高中畢業(yè)班聯(lián)考生物試題含解析
- 湖南省長(zhǎng)沙市天心區(qū)長(zhǎng)郡中學(xué)2024-2025學(xué)年高三3月月考生物試題理試卷含解析
- 山西省晉南地區(qū)達(dá)標(biāo)名校2025屆初三調(diào)研試題(一)生物試題含解析
- 浙江省金華市義烏市2025屆高三下學(xué)期第十二次重點(diǎn)考試歷史試題含解析
- 新疆新源縣2025年高中畢業(yè)生五月供題訓(xùn)練(二)化學(xué)試題含解析
- 星海音樂學(xué)院《合成生物技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 山東省濟(jì)寧地區(qū)(SWZ)重點(diǎn)中學(xué)2025年初三下學(xué)期第八次模擬考試物理試題試卷含解析
- 江蘇省南京玄武區(qū)十三中學(xué)集團(tuán)科利華2024-2025學(xué)年初三考前全真模擬密卷數(shù)學(xué)試題試卷(6)含解析
- GB/T 8237-2005纖維增強(qiáng)塑料用液體不飽和聚酯樹脂
- GB/T 14713-2009旋切機(jī)通用技術(shù)條件
- 低成本自動(dòng)化的開展與案例課件
- 不予受理反訴民事上訴狀(標(biāo)準(zhǔn)版)
- 高中英語(yǔ)語(yǔ)法之虛擬語(yǔ)氣(課件3份)
- 粵教版2022年小學(xué)六年級(jí)科學(xué)下冊(cè)期中測(cè)試試卷及答案2022-2023
- 北師大六年級(jí)下冊(cè)數(shù)學(xué)第三單元《圖形的運(yùn)動(dòng)》教學(xué)設(shè)計(jì)
- 國(guó)際石油合作主要合同模式課件
- 橋梁加固改造工程施工質(zhì)量管理體系與措施
- 第二十六章慢性腎小球腎炎演示文稿
- 設(shè)施設(shè)備維修記錄表
評(píng)論
0/150
提交評(píng)論