This article describes the implementation method of JAVA quick sorting. Share it for your reference, as follows:
package com.ethan.sort.java;import java.util.Arrays;import java.util.Iterator;import java.util.LinkedList;import java.util.List;public class QuickSort { public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) { if(arr.size()<=1) { return arr; } E pivot = arr.get(0); //Every time it is initialized, each list is different List<E> less = new LinkedList<E>(); //Pivot, this set has only one element, and is initialized every time, different List<E> pivotList = new LinkedList<E>(); List<E> more = new LinkedList<E>(); for(E i:arr){ if(i.compareTo(pivot)<0) { less.add(i); } else if(i.compareTo(pivot)>0) { more.add(i); } else { pivotList.add(i); //System.out.println("p---->"+i); } } //Recursive less = quickSort(less);//The smaller than pivot//The quicksort is performed again, and for more, it is divided into two parts more = quickSort(more); //Split less pivot more less.addAll(pivotList); //pv-------->[23], in the end there is only one element System.out.println("pv--------->"+pivotList); less.addAll(more); return less; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Integer[] arr = {23,2,8,43,22,32,4,5,34}; List l = quickSort(Arrays.asList(arr)); Iterator i = l.iterator(); while(i.hasNext()) { System.out.println(i.next()); } }}I hope this article will be helpful to everyone's Java programming.