比较Pascal中的排序类型

时间:2010-12-31 13:58:58

标签: pascal

我打算写一个程序,要求用户输入项目,然后选择排序类型对它们进行排序(冒泡,输入和选择)。 之后我必须通过时间效率来比较这些类型。 我为第一部分编写了代码,但我不知道如何在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.

我希望我能在这里找到答案......

3 个答案:

答案 0 :(得分:1)

我希望你知道Bubble,Insertion和Selection排序的时间复杂度。 如果你知道你可以像那样比较

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

如果您有其他问题,可以问我:D ...

答案 1 :(得分:0)

Pascal支持&gt;,&gt; =,=,&lt; =和&lt;为了比较,但似乎你已经知道了这一点:

        if intarray[index]<intarray[j] then

所以也许你必须更清楚地解释一下你的问题。

答案 2 :(得分:0)

我认为作者不确定如何在Pascal中测量时间。

我不知道你正在使用什么编译器,但整体模式如下:

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