快速排序Java实现

package text.algorithm;

/**
* 快速排序
* 快速排序的最差时间复杂度和冒泡排序是一样的都是O(N2),它的平均时间复杂度为O(NlogN)。
*/
public class QuickSort {
public static void quickSort(int[] a, int s, int e) {
int i,j,m;
m=a[s];
i=s;
j=e;
if(s>=e){
return;
}
while(j>i){
while(m<=a[j]&&j>i){
j--;
}
while(m>=a[i]&&j>i){
i++;
}
int t = a[j];
a[j]=a[i];
a[i]=t;
}
if(a[s]>a[j]){
int t = a[j];
a[j]=a[s];
a[s]=t;
}
quickSort(a,s,j-1);
quickSort(a,j+1,e);

}

public static void main(String[] args) {
int[] a = {1,12,21,4,6,45,14,68,54,11,3,342,5,345,3,56};
quickSort(a,0,a.length-1);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
posted @ 2019-04-12 22:25  MisMe  阅读(138)  评论(0编辑  收藏  举报