java中排序演算法的實現(一)__java

來源:互聯網
上載者:User

    轉載請註明出處:http://blog.csdn.net/xiaojimanman/article/details/17681605

     從今天起,開始寫java中關於排序演算法的實現。

     這篇就先只介紹快速排序和冒泡排序在java中的一個實現方法,其中快速排序採用遞迴實現;冒泡排序採用了for迴圈和遞迴兩種實現方式。

 /**   *@Description:  排序演算法    */ package cn.lulei.util;  import java.util.Comparator;import java.util.List;  public class SortUtil {/** * @param sortArray * @param start 數組的起始位置 * @param end 數組的結束位置 * @param c 比較子 * @Date: 2013-12-30   * @Author: lulei   * @Description: 快速排序 */public static <T> void quickSort(List<T> list, int start, int end, Comparator<? super T> c){if (list == null || start < 0 || end > (list.size() - 1) || end == start) {return; //參數有誤,直接返回}int i = start;int j = end;//一趟while (i < j) {//右側掃描while ((i < j) && (c.compare(list.get(i), list.get(j)) <= 0)){j--;}//右側掃描到比第一個比key小的交換if (i < j){T temp = list.get(i);list.set(i, list.get(j));list.set(j, temp);}//左側掃描while ((i < j) && (c.compare(list.get(i), list.get(j)) < 0)){i++;}//左側掃描到比第一個比key大的交換if (i < j){T temp = list.get(i);list.set(i, list.get(j));list.set(j, temp);}//右、左一輪結束,下一輪繼續}//一趟結束//下面是遞迴調用快速排序if (i - start > 1) {quickSort(list, start, i - 1, c);}if (end - i > 1) {quickSort(list, i + 1, end, c);}}/** * @param list * @param c * @Date: 2013-12-30   * @Author: lulei   * @Description: 快速排序 */public static <T> void quickSort(List<T> list, Comparator<? super T> c){if (list == null) {return;}quickSort(list, 0, list.size() - 1, c);}/** * @param list * @param end * @param c * @Date: 2013-12-30   * @Author: lulei   * @Description: 冒泡排序,遞迴實現方式 */public static <T> void bubbleSortRes(List<T> list, int end, Comparator<? super T> c) {if (list == null || end < 1){return;}end = end > (list.size() - 1) ? (list.size() - 1) : end;//一趟int i = 0;while (i < end) {if (c.compare(list.get(i), list.get(i + 1)) > 0) {T temp = list.get(i);list.set(i, list.get(i + 1));list.set(i + 1, temp);}i++;}//一趟結束//遞迴調用冒泡排序bubbleSortRes(list, end - 1, c);}/** * @param list * @param c * @Date: 2013-12-30   * @Author: lulei   * @Description: 冒泡排序,for迴圈實現 */public static <T> void bubbleSortFor(List<T> list, Comparator<? super T> c){if (list == null){return;}for (int i = 1; i < list.size(); i++){//標識第幾趟for (int j = 0; j < list.size() - i; j++){if (c.compare(list.get(j), list.get(j + 1)) > 0){T temp = list.get(j);list.set(j, list.get(j + 1));list.set(j + 1, temp);}//一趟結束}//排序結束}}/** * @param list * @param c * @Date: 2013-12-30   * @Author: lulei   * @Description: 冒泡排序 */public static <T> void bubbleSort(List<T> list, Comparator<? super T> c) {if (list == null){return;}bubbleSort(list, c, true);}/** * @param list * @param c 比較子 * @param rec 是否採用遞迴實現 * @Date: 2013-12-30   * @Author: lulei   * @Description: 冒泡排序 */public static <T> void bubbleSort(List<T> list, Comparator<? super T> c, boolean rec) {if (list == null){return;}if (rec) {bubbleSortRes(list, list.size(), c);} else {bubbleSortFor(list, c);}}}

     當然這兩種排序演算法中還有很多可以最佳化的部分,在以後的文章中,將會給出對應的最佳化結果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.