list,intfromIndex,inttoIndex)throwsException.." />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

快速排序List的通用方法

系統 1925 0
導讀:





  /** *//**

  * 快速排序列表中的元素,List中的元素必須實現了Comparable接口


  *


  * @param list


  * 列表


  * @param fromIndex


  * 左索引(排序開始索引)


  * @param toIndex


  * 右索引(排序結束結束索引)


  * @throws Exception


  */

  public static void quickSortList(List<comparable> list, int fromIndex, int toIndex)




  throws Exception ...{


  int tempFromIndex = fromIndex; // 左索引

  int tempToIndex = toIndex; // 右索引

  Comparable midElement; // 分割元素

  Comparable tempElement; // 臨時變量,存儲所取的列表中的元素





  if (toIndex > fromIndex) ...{






  /**//*

  * 取列表中間索引的值的對象作為分割元素


  */

  midElement = (Comparable) list.get((fromIndex + toIndex) / 2);




  // 循環列表直到索引交叉



  while (tempFromIndex <= tempToIndex) ...{




  /**//*

  * 從左索引方向開始找到第一個大于或等于分割元素的元素


  */



  while (tempFromIndex < toIndex) ...{


  tempElement = (Comparable) list.get(tempFromIndex);


  if (tempElement.compareTo(midElement) < 0)


  ++tempFromIndex;


  else

  break

  }





  /**//*

  * 從右索引方向開始找到第一個小于或等于分割元素的的元素


  */



  while (tempToIndex > fromIndex) ...{


  tempElement = (Comparable) list.get(tempToIndex);


  if (tempElement.compareTo(midElement) > 0)


  --tempToIndex;


  else

  break

  }



  // 如果索引沒有交叉則交換兩個對象位置



  if (tempFromIndex <= tempToIndex) ...{


  swap(list, tempFromIndex, tempToIndex);




  ++tempFromIndex;


  --tempToIndex;


  }

  }





  /**//*

  * 如果右索引沒有到達左索引,則排序左邊區域


  */

  if (fromIndex < tempToIndex)


  quickSortList(list, fromIndex, tempToIndex);






  /**//*

  *


  * 如果左索引沒有到達右索引,則排序右邊區域


  */

  if (tempFromIndex < toIndex)


  quickSortList(list, tempFromIndex, toIndex);




  }

  }





  /** *//**

  * 交換列表中的兩個位置的對象


  *


  * @param list


  * 列表


  * @param i


  * 索引


  * @param j


  * 索引


  */



  private static void swap(List list, int i, int j) ...{


  Object io = list.get(i);


  Object jo = list.get(j);




  list.remove(i);


  list.add(i, jo);





  list.remove(j);


  list.add(j, io);


  }
  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1896685

本文轉自
http://blog.csdn.net/mrshan/archive/2007/11/21/1896685.aspx

快速排序List的通用方法


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 灵武市| 西畴县| 武威市| 桦南县| 太湖县| 六枝特区| 北流市| 芦山县| 米脂县| 咸阳市| 澳门| 墨玉县| 南通市| 阿城市| 连南| 镇江市| 黎平县| 阳谷县| 石楼县| 漾濞| 金山区| 渭源县| 施秉县| 潜山县| 济南市| 华阴市| 和田市| 大同县| 济宁市| 科技| 奈曼旗| 永和县| 诸城市| 无锡市| 汾西县| 伽师县| 汝阳县| 抚松县| 锦州市| 贡山| 东阿县|