算法Print及所引用的數組A的值如下寫出調用Print()的運行結果(其中n=)
PROCEDURE print(iinteger)
BEGIN
IF(i<=n〉 AND (A[i] <>) THEN
BEGIN Print(*i)write(A[i])Print(*i+) END
END【合肥工業大學 四(分)】
設數組A的長度為N前N個元素A[N]遞減有序後N個元素A[N+ N]遞增有序且N是的整數次冪即k=logN為整數例如A[]=[]滿足上述要求這裡N=k=A的前個元素和後個元素分別遞減和遞增有序用此例調用如下的Demo過程並要求
()給出for循環中每次執行PerfectShuffle(AN)和CompareExchange(AN)的結果
()解釋Demo的功能 ()給出Demo的時間復雜度
PROCEDURE PerfectShuffle(VAR A:arraytype; N:integer)
[ i:=; j:=;
WHILE i<=N DO
[ B[j]:=A[i]; B[j+]:=A[i+N]; i:=i+; j:=j+; ]
A[N]:=B[N]; //B copy to A
]
PROCEDURE CompareExchange(VAR A:arraytype; N:integer)
[ j:=;
WHILE j<N DO
[ IF A[j]>A[j+] THEN A[j]←→A[j+]; //交換A[j]和A[j+]
j:=j+; ]
]
PROCEDURE Demo (VAR A:arraytype;N:integer)
//A的長度為Nk=logN為整數
[ FOR i:= TO logN DO
[ PerfectShuffle(AN); CompareExchange(AN); ]
] 【中科院計算所 四 (分)】【中國科技大學 (分)】
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/22767.html