關於QuickSorting(JAVA轉delphi程式碼) |
答題得分者是:syntax
|
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; 除了有問題的部分之外,若程式碼還有其他錯誤或是有待改進之處 可否請各位前輩不吝抽空指導一下 感激不盡 編輯記錄
pia011320 重新編輯於 2008-01-03 01:05:56, 註解 無‧
pia011320 重新編輯於 2008-01-03 01:06:48, 註解 無‧ pia011320 重新編輯於 2008-01-03 01:07:24, 註解 無‧ pia011320 重新編輯於 2008-01-03 01:08:13, 註解 無‧ pia011320 重新編輯於 2008-01-03 01:08:54, 註解 無‧ pia011320 重新編輯於 2008-01-03 01:11:39, 註解 無‧ pia011320 重新編輯於 2008-01-03 01:13:58, 註解 無‧ pia011320 重新編輯於 2008-01-03 01:15:51, 註解 無‧ |
lijenyun
一般會員 發表:3 回覆:19 積分:9 註冊:2003-06-22 發送簡訊給我 |
|
christie
資深會員 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
//TStringList也可以排序^_^
var SL:TStringList; i : Integer; begin Memo1.Lines.Clear; SL:=TStringList.Create; SL.LoadFromFile('input.txt'); for i:=0 to sl.count -1 do SL[i]:=format('m',[strtoint(SL[i])]); SL.Sort; Memo1.Lines.AddStrings(sl); SL.Free; end;
------
What do we live for if not to make life less difficult for each other? |
syntax
尊榮會員 發表:26 回覆:1139 積分:1258 註冊:2002-04-23 發送簡訊給我 |
[code delphi] repeat while (a[i] < a[l]) and (i <= r) do Inc(i); while (a[j] > a[l]) do Dec(j); temp := a[i]; a[i] := a[j]; a[j] := temp; until (i >= j); [/code] ===================引 用 pia011320 文 章=================== 各位前輩,我想請教一下以下這段程式碼該如何修改? 這是資料結構書籍上用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; 除了有問題的部分之外,若程式碼還有其他錯誤或是有待改進之處 可否請各位前輩不吝抽空指導一下 感激不盡 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |