Сравнение типов сортировки в Паскале - PullRequest
1 голос
/ 31 декабря 2010

Я собираюсь написать программу, которая просит пользователя ввести элементы, а затем выбрать тип сортировки, чтобы отсортировать их (пузырь, инсерион и выбор). После этого я должен сравнить эти типы по времени эффективности. Я написал код для первой части, но я не знал, как использовать функцию на Паскале для написания второй части (сравнение).

Вот что я сделал:

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 ]

1 голос
/ 04 января 2011

Надеюсь, вы знаете ВРЕМЯ сложности 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 ...

0 голосов
/ 03 октября 2013

Я думаю, что автор не уверен, как измерить время в Паскале.

Я не знаю, какой компилятор вы используете, но в целом шаблон выглядит так:

var
   startTime : TDateTime;
   overallTime : TDateTime;
begin
  startTime := SomeFunctionToGetCurrentTimeWithMicroseconds;
  SomeLongOperation;
  overalltime := SomeFunctionToGetCurrentTimeWithMicroseconds() - startTime;
end.
0 голосов
/ 31 декабря 2010

Pascal поддерживает>,> =, =, <= и <для сравнения, но, кажется, вы уже знаете это: </p>

        if intarray[index]<intarray[j] then

Так что, возможно, вам придется объяснить свой вопрос немного яснее.

...