Reprint please indicate the source: http://blog.csdn.net/xiaojimanman/article/details/17681605
Starting today, write Java about the implementation of the sorting algorithm.
This article will only introduce a fast sorting and bubble sort in Java implementation of a method, where the rapid sequencing using recursive implementation, bubble sort using a for loop and recursive two implementations.
/** * @Description: Sorting algorithm */package cn.lulei.util;
Import Java.util.Comparator;
Import java.util.List; public class Sortutil {/** * @param sortarray * @param start position of the start array * @param end position of the array * @param c Comparator * @D ate:2013-12-30 * @Author: Lulei * @Description: Quick Sort */public static <T> void QuickSort (list<t> l ist, int start, int end, comparator<?
Super T> C) {if List = = NULL | | Start < 0 | | end > (list.size ()-1) | |
int i = start;
int j = END; A while (I < j) {//Right scan while ((I < J) && (C.compare (List.get (i), List.get (j)) <= 0) {j-
-;
///Right scan to the exchange if (I < j) {T temp = list.get (i), which is smaller than the first key.
List.set (I, List.get (j));
List.set (J, temp);
///Left scan while ((I < J) && (C.compare (List.get (i), List.get (j)) < 0)) {i++;
///left scan to the exchange if (I < j) {T temp = list.get (i) larger than the first key; list.seT (I, List.get (j));
List.set (J, temp);
}//right and left round end, next round continue}//one trip to end//below is recursive call quick sort 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: Quick sort/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: bubble sort, hand Normalized implementation Mode * * 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;
one trip 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++;
//One trip end//recursive call bubble sort bubblesortres (list, end-1, C); /** * @param list * @param c * @Date: 2013-12-30 * @Author: Lulei * @Description: Bubble sort, for loop implementation */PU Blic static <T> void bubblesortfor (list<t> list, comparator< Super t> C) {if (list = = null) {retur
N for (int i = 1; i < list.size (); i++) {//Identify the first few for (int j = 0; J < list.size ()-I; + j) {if C.compare (li
St.get (j), List.get (j + 1)) > 0) {T temp = List.get (j);
List.set (J, List.get (j + 1));
List.set (j + 1, temp); //One end}//Sort end}/** * @param list * @param c * @Date: 2013-12-30 * @Author: Lulei * @D Escription: Bubble Sort */public static <T> void Bubblesort (list<t> List, comparator< super t> C) {if (
List = = null) {return;
} bubblesort (list, C, true); }/** * @param list * @param c Comparator * @param rec is implemented by recursion * @Date: 2013-12-30 * @Author: Lulei * @Description: Bubble sort/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);
}
}
}
Of course, there are a lot of these two sorting algorithms can be optimized, in future articles, will give the corresponding optimization results.