快速排序:
package com.test.quickSort;
public class Quick
{
public static void main(String[] args)
{
int[] a = { 21, 56, 73, 89, 44, 63, 15, 74, 10 };
sort(a);
}
private static void sort(int[] arr)
{
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++)
{
System.out.println("----" + arr[i]);
}
}
static void quickSort(int[] a, int start, int end)
{
if (start < end)
{
int flag = end;
int key = a[flag];// 以最后一个元素做key
int low = start;
int high = end;
while (low < high)
{
while (a[low] <= key && low < high)
{// 从左向右找大于或等于key的数来填s[high]
low++;
}
a[high] = a[low]; // 将s[low]填到s[high]中,s[low]就形成了一个新的坑
while (a[high] >= key && high > low)
{ // 从右向左找小于key的数来填s[low]
high--;
}
a[low] = a[high]; // 将s[high]填到s[low]中,s[high]就形成了一个新的坑
}
// 此时两个下标指向同一个元素.以这个位置左右分治(分治点)
int mid = low;
a[low] = key;// 退出时,i等于j。将x填到这个坑中
quickSort(a, start, mid - 1);
quickSort(a, mid + 1, end);
}
}
}