나는 명령형 언어로 ocaml을 사용하는 방법을 배우기보다 쉬운 예제를 사용하고 있습니다. 내 생각 엔 내가 세미콜론으로 엉망하지만 배열 정렬 명령형 ocaml
let sort array =
for index = 0 to (Array.length array -1) do
let boole = ref false;
let pos = ref index;
let max = ref array.(index);
let p = ref !pos;
let m = ref !max;
while !pos <> (Array.lenght array -1) do
if array.(!pos) > !max then begin
max := array(!pos);
boole := true;
p := !pos
end
pos := !pos + 1
done;
if (!boole = true) then begin
array.(index) <- max;
array.(pos) <- m
end
done ;;
감사합니다 코드에서 어떤 실수를 찾을 수 없습니다.
편집 1 :
let sort array =
for index = 0 to (Array.length array -1) do
let boole = ref false in
let pos = ref index in
let max = ref array.(index) in
let p = ref !pos in
let m = ref !max in
for i = !pos to (Array.length array -1) do
if (array.(i) > !max) then begin
pos :=i;
max := array.(!pos);
boole := true;
end;
done;
if (!boole = true) then begin
array.(!pos) <- !m;
array.(!p) <- !max;
end;
done ;;
질문 : 왜 bubblesort algo를 사용하지 않습니까? 첫눈에 당신이 사용하고있는 골동품을 인식하지 못합니다. –
@JoeGob 나는 방금 ocaml의 명령형 프로그래밍을 망설이고있다. 나는 최상의 알고리즘을 찾지 못했다. –