2010-12-31 7 views
1

私は、ユーザにアイテムを入力し、ソートタイプ(バブル、挿入、選択)をソートするように求めるプログラムを書くつもりです。 その後、これらのタイプを時間効率で比較する必要があります。 私は最初の部分のコードを書いたが、私はパスカルの関数を使って2番目の部分(比較)を書く方法を知らなかった。Pascalでソートタイプを比較する

これは私がやっていることです:

Begin 
    writeln('How many Number you would like to sort:'); 
    readln(size); 
    For m := 1 to size do 
    Begin 
     if m=1 then 
     begin 
      writeln(''); 
      writeln('Input the first value: '); 
      readln(IntArray[m]); 
     end 
     else if m=size then 
     begin 
      writeln('Input the last value: '); 
      readln(IntArray[m]); 
     end 
     else 
     begin 
      writeln('Input the next value: '); 
      readln(IntArray[m]); 
     End; 
    End; 

    writeln('Values Before The Sort: '); 
    for m:=0 to size-1 do 
     writeln(IntArray[m+1]); 
    writeln(); 

    begin 
     repeat 
      writeln(' ~*~...~*~ ~*~...~*~ ~*~...~*~ ~*~...~*~'); 
      writeln('1. Insertion Sort.'); 
      writeln('2. Bubble Sort.'); 
      writeln('3. Selection Sort. '); 
      writeln('4. Exist '); 
      writeln(''); 
      writeln('Enter your choice number: '); 
      readln(sort); 
      case sort of 
       1: begin {when choice = 1} 
         writeln(''); 
         writeln(' The sorted numbers by Insertion sort are ~> '); 
         For i := 2 to size do 
         Begin 
          index := intarray[i]; 
          j := i; 
          While ((j > 1) AND (intarray[j-1] > index)) do 
          Begin 
           intarray[j] := intarray[j-1]; 
           j := j - 1; 
          End; 
          intarray[j] := index; 
         End; 
         for i:= 1 to size do 
          writeln(intarray[i]); 
        end; 
       2: begin {when choice = 2} 
         writeln(''); 
         writeln(' The sorted numbers by bubble sort are ~> '); 

         For i := size-1 DownTo 1 do 
          For j := 2 to i do 
           If (intArray[j-1] > intarray[j]) then 
           Begin 
            temp := intarray[j-1]; 
            intarray[j-1] := intarray[j]; 
            intarray[j] := temp; 
           End; 
         for i:= 1 to size do 
          writeln(intarray[i]); 
        end; 

       3: begin {when choice = 3} 
         writeln(''); 
         writeln(' The sorted numbers by selection sort are ~> '); 
         for i:=1 to size do 
         begin 
          j:= i ; 
          for index:= i +1 to size do 
           if intarray[index]<intarray[j] then 
            j:=index; 
          temp:=intarray[j]; 
          intarray[j]:=intarray[i]; 
          intarray[i]:=temp; 
         end; 
         for i:= 1 to size do 
          writeln(intarray[i]); 
        end; 

       4: begin 
         writeln('*~...~*~ Thank U For used Our Program We Hope You Enjoyed ~*~...~*~ '); 
        end; 
      end; 
     until sort = 4 ; 
    end; 
end. 

私は私がここで答えを見つけることを願って...

答えて

0

パスカルは、>、> =、=サポート< =との比較のために<しかし、あなたはすでにこれを知っているようです:

 if intarray[index]<intarray[j] then 

多分あなたは少し明確にあなたの質問を説明する必要があります。

1

バブル、挿入、選択ソートのTIMEの複雑さを知っていれば幸いです。 D ...

+0

はどうもありがとうございました、しかし、あなたは、時間の複雑さのバブ、インとSELCを取得する方法を教えてくださいだろうか?私は時間効率O(N)について知っています、Nは2ループ時間効率= O(N2)であれば、使用するループの数に依存します。しかし、私は正確にその時代を見つけて、あなたが書いた前のコードのようにそれらを比較する方法を知らなかった... – data

0

私は著者がパスカルでの時間を測定する方法がわからないと思う:あなたが知っている場合 あなたはちょうどあなたが他の質問がある場合は、あなたが私に尋ねることができる

if (time_complexity_bub>time_complexity_ins)and(time_complexity_bub>time_complexity_sel) then writeln('Bubble Sort is the WORST !!!'); 

    if (time_complexity_ins>time_complexity_bub)and(time_complexity_ins>time_complexity_sel) then writeln('Insertion Sort is the WORST !!!'); 

    if (time_complexity_sel>time_complexity_ins)and(time_complexity_bub<time_complexity_sel) then writeln('Selection Sort is the WORST !!!'); 

のように比較することができます。

私はあなたが使用しているものコンパイラ分かりませんが、全体的なパターンは次のようである:

var 
    startTime : TDateTime; 
    overallTime : TDateTime; 
begin 
    startTime := SomeFunctionToGetCurrentTimeWithMicroseconds; 
    SomeLongOperation; 
    overalltime := SomeFunctionToGetCurrentTimeWithMicroseconds() - startTime; 
end.