研发之余,把代码过程中比较常用的内容做个备份,下面的资料是关于java快速排序算法的代码,应该能对小伙伴们有些帮助。
package Mypackage;
public class QuickSort {
public static void main(String[] args) {
int[] arr = { 2, 5, 4, 3, 7, 0, 9, 1, 6, 8 };
quickSort(arr, 2, 7);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
public static void quickSort(int[] arr, int low, int high) {
int i = 0, j, key = 0;
if (low < high) {
key = arr[low];
i = low;
j = high;
while (i < j) {
while (i < j && arr[j] > key)
j--;
if (i < j)
arr[i++] = arr[j];
while (i < j && arr[i] < key)
i++;
if (i < j)
arr[j--] = arr[i];
}
}
arr[i] = key;
if (i > low + 1)
quickSort(arr, low, i - 1);
if (i < high - 1)
quickSort(arr, i + 1, high);
}
复制代码
}