首页 > 代码库 > Quick Sort in Java and Erlang
Quick Sort in Java and Erlang
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.
Quick Sort in Java and Erlang
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。