2010-12-31 8 views
1

Ich werde ein Programm schreiben, das den Benutzer auffordert, Elemente einzugeben, und dann den Sortiertyp auswählt, um sie zu sortieren (Blase, Einfügung und Auswahl). Danach muss ich diese Typen durch Zeiteffizienz vergleichen. Ich schrieb Code für den ersten Teil, aber ich wusste nicht, wie man eine Funktion in Pascal verwendet, um den zweiten Teil zu schreiben (Vergleich).Vergleichen von Sortierungstypen in Pascal

Dies ist, was ich getan habe:

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. 

Ich hoffe, dass ich hier die Antwort finden ...

Antwort

0

Pascal unterstützt>,> =, =, < = und < zum Vergleich, aber es scheint, dass Sie das bereits wissen:

 if intarray[index]<intarray[j] then 

Also vielleicht müssen Sie Ihre Frage ein wenig klarer zu erklären.

1

Ich hoffe, dass Sie die TIME-Komplexität von Bubble, Insertion und Selection sort kennen. Wenn Sie wissen, Sie können einfach so

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 !!!'); 

vergleichen Wenn Sie weitere Fragen haben, können Sie mich fragen: D ...

+0

danke sehr viel, aber könnten Sie mir bitte sagen, wie die Zeit Komplexität bub, Ins und selc bekommen? Ich weiß über Zeiteffizienz O (N), N hängt davon ab, wie viele Schleife ich verwenden werde, wenn es 2 Schleife Zeit Effizienz = O (N2) ist. aber ich wusste nicht wie genau ich das mal finde und vergleiche zwischen ihnen wie der vorhergehende Code den du geschrieben hast ... – data

0

Ich denke Autor nicht sicher ist, wie die Zeit in Pascal zu messen.

Ich weiß nicht, welchen Compiler Sie verwenden, aber alles in allem Muster wie:

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