快速排序(java版)

2014-11-24 02:57:47 · 作者: · 浏览: 1

今天心血来潮,想练练自己的基本功,就写了快排程序,唯一注意的一点就是
System.out.print(a[i]+‘ ’);如果a[i]为整数,那么这个输出的结果将是空格的ascII码,空格与字符串相连接时才会输出空格。
view plain
import java.util.*;

public class QuickSort {

/**
* @param args
*/
static void quickSort(int[] a, int front, int rear) {

if(front {

int flag = rear;

int temp = a[flag];

int p = front;
int q = rear;


while (p

while (a[p] <= temp && p < q) {
p++;
}
a[q] = a[p];
while (a[q] >= temp && q > p) {
q--;
}
a[p] = a[q];
}
int mid=p;
a[p] = temp;

quickSort(a, front, mid-1);
quickSort(a, mid+1, rear);
}

}

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a={3,2,1,4};
quickSort(a,0,a.length-1);
for(int i=0;i System.out.print(a[i]);
}



}

}

摘自

:不喜欢熬夜的coder