本帖最后由 赵喜平 于 2013-4-4 11:45 编辑
- class Arr {
- public static void main(String args[])
- {
- int arr[] = { 5, 3, 8, 2, 0, 9, 1, 7 };//35208179,
- quick(arr, 0, (arr.length-1));
-
- printArr(arr);
- //
- }
-
- public static void quick(int arr[],int left,int right)
- {
-
- if(left < right)
- {
-
- int temp = getStart(arr,left,right); //此处?
- quick(arr,left,temp); //此处?
- quick(arr,temp+1,right);
- }
- }
- public static int getStart(int arr[],int right,int left)
- {
- int i = 0,j = 0,temp = 0;
- int key = 0;
-
- if(arr == null) { return 0; }
-
- i = left;
- j = right;
- key = arr[left];
-
- while(i < j)
- {
- while(arr[j] > key)
- {
- j--;
- }
-
- swap(arr,j,i);
- while(arr[i] < key)
- {
- i++;
- }
-
- swap(arr,i,j);
- }
-
- return i;
- }
-
- public static void swap(int arr[],int i,int j)
- {
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- public static void printArr(int arr[]) {
- for (int i : arr)
- {
- System.out.print(i + " ");
- }
- System.out.println();
- }
- }
复制代码 |