程式碼改寫(JAVA→DELPHI) |
答題得分者是:christie
|
pia011320
一般會員 發表:11 回覆:6 積分:3 註冊:2007-07-17 發送簡訊給我 |
各位前輩,我想請教一下以下這段程式碼該如何修改?
這是資料結構書籍上用JAVA寫的QuickSorting的範例 int partition(int a[], int l, int r) // 對a[l]..a[r]進行分割,傳回分割點 { int i, j, temp; for (i =l 1, j =r ; ;) { while (a[i] < a[l] && i <= r) // i 一路往右 i ; while (a[j] > a[l]) // j 一路往左 j--; if ( i >= j ) //如果交錯則跳出迴圈 break; temp = a[i]; a[i] = a[j]; a[j] = temp; } //for temp = a[l]; // 最後一次交換 a[l] = a[j]; a[j] =temp; return(j); } void Quick_Sort(int a[], int l, int r) { int partition_point; if ( l < r ) { partition_point = partition( a, l, r ); //分割 Quick_Sort(int a, l, partition_point - 1); //收拾左半部 Quick_Sort(int a, partition_point 1, r); //收拾右半部 } } 以下是我修改過的程式碼 unit QuickSort; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; private { Private declarations } public { Public declarations } end; type aaa = array of integer; var Form1: TForm1; a:aaa; implementation {$R *.dfm} procedure Quick_Sort(a: aaa; l,r: integer); var partition_point:integer; function partion(a: aaa; i,j:integer): integer; var temp: integer; begin // for i := l 1 j = r do <----有問題的部份 begin while (a[i] < a[l]) and (i<=r) do begin i:= i 1; while a[j] > a[l] do begin j:= j-1; if i >= j then break else begin temp:= a[i]; a[i]:= a[j]; a[j]:= temp; end; end; partion := j; end; temp:=a[l]; a[l]:=a[j]; a[j]:=temp; end; end; begin if l < r then partition_point := partion(a, l, r); Quick_Sort(a, l, partition_point-1); Quick_Sort(a, partition_point 1, r); end; 除了有問題的部分之外,若程式碼還有其他錯誤或是有待改進之處 可否請各位前輩不吝抽空指導一下 感激不盡 |
Clarinet
高階會員 發表:9 回覆:97 積分:126 註冊:2002-05-08 發送簡訊給我 |
|
pia011320
一般會員 發表:11 回覆:6 積分:3 註冊:2007-07-17 發送簡訊給我 |
感謝這位前輩的協助
經過修改之後: procedure Quick_Sort(a: aaa; l,r: integer); var partition_point:integer; function partion(a: aaa; i,j:integer): integer; var temp: integer; begin for i:=l 1 to r do begin while (a[i] < a[l]) and (i<=r) do begin i:= i 1; ←───錯誤訊息:Left side cannot be assigned to while a[j] > a[l] do begin j:= j-1; if i >= j then break else begin temp:= a[i]; a[i]:= a[j]; a[j]:= temp; end; end; partion := j; end; temp:=a[l]; a[l]:=a[j]; a[j]:=temp; end; end; ===================引 用 Clarinet 文 章=================== 沒看完全部的程式碼,只針對你提出的問題點來回答 你貼上的JAVA碼 for (i =l 1, j =r ; ;) 應該沒這樣的用法吧!是否打錯了。 正確是否應為 for (i =l 1; i=r; i ) 轉成Delphi為 for i:=l 1 to r do begin end; |
christie
資深會員 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
(* Implementing QuickSort Sorting Algorithm in Delphi *)
[code delphi] procedure QuickSort(var A: array of Integer; iLo, iHi: Integer) ; var Lo, Hi, Pivot, T: Integer; begin Lo := iLo; Hi := iHi; Pivot := A[(Lo Hi) div 2]; repeat while A[Lo] < Pivot do Inc(Lo) ; while A[Hi] > Pivot do Dec(Hi) ; if Lo <= Hi then begin T := A[Lo]; A[Lo] := A[Hi]; A[Hi] := T; Inc(Lo) ; Dec(Hi) ; end; until Lo > Hi; if Hi > iLo then QuickSort(A, iLo, Hi) ; if Lo < iHi then QuickSort(A, Lo, iHi) ; end; [/code] procedure TForm1.Button1Click(Sender: TObject); var intArray : array of integer; i:word; begin SetLength(intArray,10) ; //Add values to intArray intArray[0] := 2007; intArray[1] := 2017; intArray[2] := 2217; intArray[3] := 2010; intArray[4] := 2117; intArray[5] := 2012; intArray[6] := 2213; intArray[7] := 2014; intArray[8] := 2115; intArray[9] := 1973; //sort QuickSort(intArray, Low(intArray), High(intArray)) ; end; 參考來源 http://delphi.about.com/od/objectpascalide/a/quicksort.htm
------
What do we live for if not to make life less difficult for each other?
編輯記錄
christie 重新編輯於 2008-01-03 14:37:21, 註解 無‧
|
pia011320
一般會員 發表:11 回覆:6 積分:3 註冊:2007-07-17 發送簡訊給我 |
謝謝這位前輩的解答
另外,小弟想再問一個問題 如果想把排序好的資料另存成一個.txt檔 程式該如何寫? 再次感謝各位前輩的指導 ===================引 用 christie 文 章=================== 參考來源 http://delphi.about.com/od/objectpascalide/a/quicksort.htm |
christie
資深會員 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
var
j : Integer; SL:TStringList; begin SL:=TStringList.Create; //Length(intArray) : Return the number of elements in an array for j:=0 to Length(intArray)-1 do begin SL.Add(inttostr(inttostr(intArray[j]))) end; SL.SaveToFile('QSort.txt'); SL.Free; end; ===================引 用 pia011320 文 章=================== 謝謝這位前輩的解答 另外,小弟想再問一個問題 如果想把排序好的資料另存成一個.txt檔 程式該如何寫? 再次感謝各位前輩的指導
------
What do we live for if not to make life less difficult for each other? |
Coffee
版主 發表:31 回覆:878 積分:561 註冊:2006-11-15 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |