1
votes

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

3

3 Answers

1
votes

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

0
votes

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.

0
votes

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.