java算法----排序----(4)快速排序

 package log;

 public class Test4 {

     /**
* java算法---快速排序
*
* @param args
*/
public static void main(String[] args) {
// 需要排序的数组
int arr[] = { 49, 20, 36, 51, 18, 94, 61, 31, 50 };
// 循环输出该数组内容
System.out.println("排序之前:");
for (int a : arr) {
System.out.print(a + "\t");
}
System.out.println(); if (arr.length > 0) {
sort(arr, 0, arr.length - 1);
}
// 循环输出该数组内容
System.out.println("排序之后:");
for (int a : arr) {
System.out.print(a + "\t");
}
System.out.println(); } public static int getMiddle(int[] list, int low, int high) {
int temp = list[low];
while (low < high) {
while (low < high && list[high] >= temp) {
high--;
}
list[low] = list[high];
while (low < high && list[low] <= temp) {
low++;
}
list[high] = list[low];
}
list[low] = temp;
return low;
} public static void sort(int[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high);
sort(list, low, middle - 1);
sort(list, middle + 1, high);
}
} }

下面这事控制台的输出

java算法----排序----(4)快速排序

上一篇:ASP.NET Core 6框架揭秘实例演示[12]:诊断跟踪的进阶用法


下一篇:使用MySQL的LAST_INSERT_ID--转