比较 Pascal 中的排序类型

发布于 2024-10-09 17:18:09 字数 3655 浏览 3 评论 0原文

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

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

I am going to write a program that asks the user to enter items then choose the sorting type to sort them (bubble, inserion and selection).
After that I have to compare these types by time efficiency.
I wrote code for the first part, but I didn't know how to use a function in Pascal to write the second part (comparison).

This is what I have done:

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.

I hope that I will find the answer here...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-10-16 17:18:09

我希望你知道冒泡排序、插入排序和选择排序的时间复杂度。
如果你知道你可以这样比较

   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 ...

I hope you know the TIME complexity of Bubble, Insertion and Selection sort.
If you know you can just compare like that

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

If you have other questions you can ask me :D ...

似狗非友 2024-10-16 17:18:09

Pascal 支持 >、>=、=、<= 和 <进行比较,但似乎您已经知道这一点:

        if intarray[index]<intarray[j] then

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

Pascal supports >,>=,=,<= and < for comparison, but it seems you already know this:

        if intarray[index]<intarray[j] then

So maybe you have to explain your question a bit clearer.

戒ㄋ 2024-10-16 17:18:09

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

我不知道你使用的是什么编译器,但总体模式是这样的:

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

I think author is not sure how to measure time in Pascal.

I don't know what compiler you're using, but overall pattern is like:

var
   startTime : TDateTime;
   overallTime : TDateTime;
begin
  startTime := SomeFunctionToGetCurrentTimeWithMicroseconds;
  SomeLongOperation;
  overalltime := SomeFunctionToGetCurrentTimeWithMicroseconds() - startTime;
end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文