2010-12-31 6 views
1

Je vais écrire un programme qui demande à l'utilisateur d'entrer des items puis de choisir le type de tri pour les trier (bulle, inserion et sélection). Après cela, je dois comparer ces types par l'efficacité du temps. J'ai écrit du code pour la première partie, mais je ne savais pas comment utiliser une fonction en Pascal pour écrire la deuxième partie (comparaison).Comparer les types de tri en Pascal

Voici ce que je l'ai fait:

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. 

J'espère que je trouverai la réponse ici ...

Répondre

0

Pascal soutient>,> =, =, < = et < à titre de comparaison, mais il semble que vous le sachiez déjà:

 if intarray[index]<intarray[j] then 

Alors peut-être que vous devez expliquer votre question un peu plus clairement.

1

J'espère que vous connaissez la complexité TIME du tri Bubble, Insertion et Selection. Si vous savez que vous pouvez les comparer comme ça

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

Si vous avez d'autres questions, vous pouvez me demander: D ...

+0

merci beaucoup, mais pourriez-vous s'il vous plaît me dire comment obtenir le temps, la complexité Bub ins et selc? Je connais l'efficacité du temps O (N), N dépend du nombre de boucles que j'utiliserai, si c'est 2 temps de boucle efficacité = O (N2). mais je ne savais pas exactement comment trouver ces temps et comparer entre eux comme le code précédent que vous avez écrit ... – data

0

Je pense que l'auteur ne sait pas comment mesurer le temps en Pascal.

Je ne sais pas quel compilateur vous utilisez, mais l'ensemble est comme modèle:

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