2012-03-18 59 views
2

我有一個項目,程序必須接受10個單詞,並按降序(從Z-A的字母順序) 使用氣泡排序顯示單詞。 以下是我目前所知道的: 程序示例; 使用crt;Pascal Bubble排序

TYPE 
    no._list=ARRAY(1...10)OF REAL; 
CONST 
    no.:no._list=(20.00,50.50.35.70....); 
VAR 
    x:INTEGER; 
    small:REAL; 

BEGIN clrscr: 
    small:=no.(1); 
    FOR x:=2 TO 10 DO 
     IF small>number(x); 
     writeln('Smallest value in the array of no.s is',small:7:2); 
END 

我真的不知道如何做到這一點,但可以使用一些幫助。

+2

你嘗試過什麼?你有什麼具體問題?我們在這裏幫助解決問題,而不是爲你做功課。 – Corbin 2012-03-18 08:23:02

+0

你知道什麼? – Doboy 2012-03-18 08:26:03

+0

哦..對不起..我們的老師只教我們泡泡分類號..這實際上是我們的項目。她實際上並沒有討論這件事,但她把它作爲一個項目給了它。 – 2012-03-18 08:26:50

回答

0
function BubbleSort(list: TStringList): TStringList; 
var 
    i, j: Integer; 
    temp: string; 
begin 
    // bubble sort 
    for i := 0 to list.Count - 1 do begin 
    for j := 0 to (list.Count - 1) - i do begin 
     // Condition to handle i=0 & j = 9. j+1 tries to access x[10] which 
     // is not there in zero based array 
     if (j + 1 = list.Count) then 
     continue; 
     if (list.Strings[j] > list.Strings[j+1]) then begin 
     temp    := list.Strings[j]; 
     list.Strings[j] := list.Strings[j+1]; 
     list.Strings[j+1] := temp; 
     end; // endif 
    end; // endwhile 
    end; // endwhile 
    Result := list; 
end;