快排(用递归实现)- public class MySort
- {
- public static void main(String[] args)
- {
- int[] arr = {1,6,7,5,3,5,8,9,7,56};
-
-
- QuickSort.Sort(arr, 0, arr.length-1);
-
-
- for (int i = 0; i < arr.length; ++i)
- {
- System.out.println(arr[i]);
- }
- }
- }
- class QuickSort
- {
- public static void Sort(int arr[], int low, int high)
- {
- int pos = 0;
-
- if (low < high)
- {
- pos = FindPos(arr, low, high);
- Sort(arr, low, pos-1);
- Sort(arr, pos+1, high);
- }
- }
-
- public static int FindPos(int arr[], int low, int high)
- {
- int val = arr[low];
- while (low < high)
- {
- while (low < high && arr[high] >= val)
- --high;
- arr[low] = arr[high];
-
- while (low < high && arr[low] <= val)
- ++low;
- arr[high] = arr[low];
- }
- arr[low] = val;
- return low;
- }
- }
复制代码 |