快速排序

来源:互联网 发布:电视的mac地址怎么查 编辑:程序博客网 时间:2024/05/16 03:18
public static void quickSorted(ArrayList<Integer> lists) {
  if (lists.size() > 0) {
   ArrayList<Integer> larger = new ArrayList<Integer>();
   ArrayList<Integer> same = new ArrayList<Integer>();
   ArrayList<Integer> smaller = new ArrayList<Integer>();
   int Chosen = lists.get(lists.size() / 2);
   for (int i = 0; i < lists.size(); i++) {
    if (lists.get(i) > Chosen) {
     larger.add(lists.get(i));
    } else if (lists.get(i) == Chosen) {
     same.add(lists.get(i));
    } else {
     smaller.add(lists.get(i));
    }
   }
   quickSorted(larger);
   quickSorted(smaller);
   lists.clear();
   lists.addAll(smaller);
   lists.addAll(same);
   lists.addAll(larger);
  }
 }
0 0
原创粉丝点击