国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

排序算法

2019-11-08 18:27:51
字體:
來源:轉載
供稿:網友

1.插入排序-直接插入排序

public class Test { public static void main(String[] args) { int[] array = {23,34,45,234,23,34,46,732,7,4,24,45}; insertSort(array); } public static void insertSort(int[] a){ for(int i=1; i<a.length; i++){ if (a[i] < a[i-1]){ int j = i-1; int flag = a[i]; while(flag < a[j]){ a[j+1] = a[j]; j--; if(j == -1){ break; } } a[j+1] = flag; } PRint(a); } } private static void print(int[] a) { for (int i : a) { System.out.print(i+" "); } System.out.println(); }}

運行結果: 23 34 45 234 23 34 46 732 7 4 24 45 23 34 45 234 23 34 46 732 7 4 24 45 23 34 45 234 23 34 46 732 7 4 24 45 23 23 34 45 234 34 46 732 7 4 24 45 23 23 34 34 45 234 46 732 7 4 24 45 23 23 34 34 45 46 234 732 7 4 24 45 23 23 34 34 45 46 234 732 7 4 24 45 7 23 23 34 34 45 46 234 732 4 24 45 4 7 23 23 34 34 45 46 234 732 24 45 4 7 23 23 24 34 34 45 46 234 732 45 4 7 23 23 24 34 34 45 45 46 234 732

2.插入排序-希爾排序

public class ShellTest { public static void main(String[] args) { int[] array = {23,34,45,234,23,34,46,732,7,4,24,45}; shellSort(array); } private static void shellSort(int[] a) { int half = a.length/2; while(half>=1){ shellsort(a,half); half=half/2; } } private static void shellsort(int[] a, int half) { for(int i= half; i<a.length; ++i){ if(a[i] < a[i-half]){ //若第i個元素大于i-1元素,直接插入。小于的話,移動有序表后插入 int j = i-half; int x = a[i]; //復制為哨兵,即存儲待排序元素 a[i] = a[i-half]; //首先后移一個元素 while(x < a[j]){ //查找在有序表的插入位置 a[j+half] = a[j]; j -= half; //元素后移 if(j < -1){ break; } } a[j+half] = x; //插入到正確位置 } print(a); } } private static void print(int[] a) { for (int i : a) { System.out.print(i+" "); } System.out.println(); }}

3.選擇排序-簡單選擇排序

/** * 選擇排序簡單選擇排序 * * * @author baoyu * */public class SimpleSelectionSort { public static void main(String[] args) { int[] array = {23,34,45,234,23,34,46,732,7,4,24,45}; simpleSelectionSort(array); } /** * 1. 第一次在n中先找出最小的值,與第一位交換 * 2. 第二次在2~n中找出最小的值,與第二位交換 * 3. 以此類推......直到交換到n-1位置時結束 * @param a */ private static void simpleSelectionSort(int[] a) { for(int i=0; i<a.length-1; i++){ int min = a[i]; int flag = i; for( int j = i; j<a.length-1; j++){ if(a[j+1] < min){ min = a[j+1]; flag = j+1; } } a[flag] = a[i]; a[i] = min; print(a); } } private static void print(int[] a) { for (int i : a) { System.out.print(i+" "); } System.out.println(); }}

運行結果: 4 34 45 234 23 34 46 732 7 23 24 45 4 7 45 234 23 34 46 732 34 23 24 45 4 7 23 234 45 34 46 732 34 23 24 45 4 7 23 23 45 34 46 732 34 234 24 45 4 7 23 23 24 34 46 732 34 234 45 45 4 7 23 23 24 34 46 732 34 234 45 45 4 7 23 23 24 34 34 732 46 234 45 45 4 7 23 23 24 34 34 45 46 234 732 45 4 7 23 23 24 34 34 45 45 234 732 46 4 7 23 23 24 34 34 45 45 46 732 234 4 7 23 23 24 34 34 45 45 46 234 732


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳城县| 北碚区| 府谷县| 济南市| 新津县| 郁南县| 思南县| 四子王旗| 大渡口区| 苗栗市| 贵定县| 原阳县| 唐山市| 临颍县| 博白县| 会理县| 新宾| 尉犁县| 凉山| 屯留县| 紫金县| 博爱县| 荆门市| 夹江县| 广州市| 新闻| 和政县| 双城市| 买车| 济源市| 东港市| 上饶市| 奎屯市| 佛坪县| 密云县| 钟祥市| 汨罗市| 彰化县| 苍山县| 南投市| 白山市|