多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > 排序算法總結

排序算法總結

來源:程序員人生   發布時間:2015-06-05 08:51:44 閱讀次數:3250次
代碼寫久了,越發的覺得寫到后來回歸的都是基礎。頓時覺得后悔大1大2沒好好學這些計算機基礎課程,虧大了。
總結下排序算法:
package 排序算法; /** * 1.選擇排序 * 2.插入排序 * 3.歸并排序 * 4.快速排序 * * @author Administrator * */ public class 4種排序算法 { public static void main(String[] args) { int[] arr = { 2, 5, 6, 7, 4, 3, 1, 1, 3, 4, 5, 1, 3, 6, 3 }; // selectSort(arr); // insertedSort(arr); // mergeSort(arr, 0, arr.length - 1); quickSort(arr, 0, arr.length - 1); out(arr); } // 輸出 public static void out(int[] arr) { for (int e : arr) System.out.print(e + ","); } // 1.選擇排序 public static void selectSort(int[] a) { for (int i = 0; i < a.length; i++) for (int j = i + 1; j < a.length; j++) { int key = a[i]; if (key > a[j]) { a[i] = a[j]; a[j] = key; } } } // 2.插入排序 public static void insertedSort(int[] a) { for (int i = 1; i < a.length; i++) { int key = a[i]; int j = i - 1; while (j >= 0 && key < a[j]) { a[j + 1] = a[j]; a[j] = key; j--; } } } // 3.歸并排序(分治法) public static void mergeSort(int[] a, int low, int high) { if (low < high) { int mid = (low + high) / 2; // 遞歸到最小原子 mergeSort(a, low, mid); mergeSort(a, mid + 1, high); // 合并數組 mergeArray(a, low, mid, high); } } // 合并數組 private static void mergeArray(int[] a, int low, int mid, int high) { int N = high - low; int[] temp = new int[N + 1]; int i = low, j = mid + 1; int m = mid, n = high; int k = 0; while (i <= m && j <= n) temp[k++] = (a[i] < a[j] ? a[i++] : a[j++]); while (i <= m) temp[k++] = a[i++]; while (j <= n) temp[k++] = a[j++]; for (i = 0; i < k; i++) a[low + i] = temp[i]; } // 4.快速排序(分治法) public static void quickSort(int[] a, int low, int high) { if (low < high) { int r = adjustArray(a, low, high); quickSort(a, low, r - 1); quickSort(a, r + 1, high); } } // 提取數組中的首個元素,分割。小的元素放在<strong>標志</strong>的左側,比 // 標志大的元素放在其右側,最后返回標志元素的位置 private static int adjustArray(int[] a, int low, int high) { int i = low, j = high; int spcValue = a[low]; while (i < j) { while (a[j] >= spcValue && i < j) j--; if (a[j] < spcValue) a[i++] = a[j]; while (a[i] <= spcValue && i < j) i++; if (a[i] > spcValue) a[j--] = a[i]; } a[i] = spcValue; return i; } }


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产h在线 | 亚洲免费视频网站 | 中文乱码字字幕在线第5页 中文欧美日韩 | www.黄色网址.com | 国产免费一区二区三区在线 | 最新jizz 欧美 | 欧美自拍网站 | 黑人猛交 | 中文字幕.com| 日韩 欧美 自拍 | 波多野结衣中文字幕在线视频 | 亚洲精品一区二区乱码在线观看 | 在线观看中文字幕 | 免费成年人视频在线观看 | 欧美第一福利 | 国产成人精品免费视频网页大全 | 国产精品天天看天天爽 | 美美女高清毛片视频免费观看 | 男女免费爽爽爽在线视频 | 欧美国产亚洲一区 | xxxx欧美 | 伊人久久成人成综合网222 | 最新更新国内自拍视频 | 中文字幕不卡一区 二区三区 | 久久精品一品道久久精品9 久久精品一区二区 | 天天综合视频网 | 最近中文字幕完整视频大全版 | 欧美一级毛片无遮挡内谢 | 免费黄网址 | 欧美伦理一区 | 美国免费毛片 | 精品女人 | 一区二区三区免费观看 | 中文字幕区 | 亚洲成人贴图 | 又黄又爽又色的性视频 | 在线观看美女网站 | 国产日产欧产精品精品推荐小说 | 五月激情五月婷婷 | 欧美xxxxx性另类 | 美国美女一级片 |