1 import java.util.*; 2 3 public class Main { 4 5 // static void swap(int a, int b) { 交换的另外一种方式,不过这里不能用静态,会交换异常,失败 6 // a = a ^ b; 7 // b = a ^ b; 8 // a = a ^ b; 9 // } 10 11 static void quicksort(int[] a, int i, int j) { 12 if(i >= j)return; 13 int x = 0; 14 int temp = a[i]; 15 int low = i; 16 int high = j; 17 18 while(low < high) { 19 while(a[high] >= temp && low < high) high--; 20 while(a[low] <= temp && low < high) low++; 21 if(low < high) { 22 x = a[low]; 23 a[low] = a[high]; 24 a[high] = x; 25 } 26 } 27 a[i] = a[high]; 28 a[high] = temp; 29 30 31 quicksort(a, i, high-1); 32 quicksort(a, high+1, j); 33 } 34 35 36 37 public static void main(String args[]) { 38 Scanner sc = new Scanner(System.in); 39 int[] c = new int[11]; 40 int sign = 1; 41 int index = 0; 42 while(true) { 43 sign = sc.nextInt(); 44 if(sign == 0) break; 45 c[index++] = sign; 46 } 47 int[] b = new int[index]; 48 b = Arrays.copyOfRange(c, 0, index); 49 50 quicksort(b, 0, b.length-1); 51 for(int i = 0; i < b.length; i++) 52 System.out.print(b[i] + " "); 53 } 54 }
重新认识了一些细节,仅记录。
原文:https://www.cnblogs.com/ohuo/p/12323732.html