标签:style java io strong ar for art sp amp
Quick Sort In Java.
public static void quickSort(int[] a, int p, int r) { if (p < r) { int q = partition(a, p, r); quickSort(a, p, q); quickSort(a, q + 1, r); } } private static int partition(int[] a, int p, int r) { int x = a[p]; int i = p - 1; int j = r + 1; while (true) { i++; while (i < r && a[i] < x) i++; j--; while (j > p && a[j] > x) j--; if (i < j) swap(a, i, j); else return j; } } private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; }
Quick Sort in Erlang
%% much easier than Java. q_sort([]) -> []; q_sort([Pivot|T]) -> q_sort([X || X <- T, X < Pivot ]) ++ [Pivot] ++ q_sort([X || X <- T, X >= Pivot]).
For me, Erlang is easier to understand.
extended question:
for given number N, find {A,B,C} which A+B+C<N and A*A + B*B = C*C.
pyth_list(N) -> [{A,B,C} || A <- lists:seq(1, N), B <- lists:seq(1, N), C <- lists:seq(1, N), A+B+C =< N, A*A + B*B =:= C*C ].
For me, Java would need much more code.
标签:style java io strong ar for art sp amp
原文地址:http://my.oschina.net/u/1767744/blog/307811