快速排序

来源:互联网 发布:c语言输出txt文件内容 编辑:程序博客网 时间:2024/06/05 02:13
public class QuickSort {public void quickSort(int[] a, int left, int right){int i, j, t, temp;if (left > right)return;temp = a[left]; //哨兵i = left;j = right;while (i != j){while (a[j] >= temp && i < j) //从右往左找j--;while (a[i] <= temp && i < j) //从左往右找i++;if (i < j){t = a[i];a[i] = a[j];a[j] = t;}}a[left] = a[i];a[i] = temp;quickSort(a, left, i-1);quickSort(a, i+1, right);}public static void main(String[] args){//int[] arr = {8, 3, 1, 0, 5, 6, 2, 7, 1};int[] arr = {25, 15,27,99,18,35,14,66};new QuickSort().quickSort(arr, 0, arr.length-1);for(int i : arr){System.out.println(i);}}}