Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Sort;
- {$APPTYPE CONSOLE}
- Type
- TArr = Array Of Integer;
- Var
- N: Integer;
- NumArr: TArr;
- Procedure OutputArray(Arr: TArr);
- Var
- I: Integer;
- Begin
- for I := Low(Arr) to High(Arr) do
- Write(Arr[I]:4);
- Writeln;
- End;
- Function CreateArr(N: Integer): TArr;
- Var
- I: Integer;
- Arr: TArr;
- Begin
- SetLength(Arr, N);
- for I := Low(Arr) to High(Arr) do
- Arr[I] := Random(100);
- CreateArr := Arr;
- End;
- Procedure SimpleBubbleSort;
- Var
- A: TArr;
- I, J, Temp: Integer;
- ComparisonsCount, AssignmensCount, CalculationsCount: Integer;
- Begin
- A := Copy(NumArr);
- ComparisonsCount := 0; // сравнения
- AssignmensCount := 0; // присваивания
- CalculationsCount := 0; // вычисления
- For J := Low(A) to High(A)-1 do
- Begin
- Inc(AssignmensCount);
- Inc(CalculationsCount);
- Inc(ComparisonsCount);
- For I := Low(A)+1 to High(A)-J do
- Begin
- Inc(AssignmensCount);
- Inc(CalculationsCount);
- if A[I-1] > A[I] then
- Begin
- Temp := A[I];
- A[I] := A[I-1];
- A[I-1] := Temp;
- Inc(AssignmensCount, 3);
- End;
- Inc(ComparisonsCount);
- End;
- End;
- Writeln('SimpleBubbleSort:');
- //OutputArray(A);
- Writeln('Количество операций сравнения: ', ComparisonsCount);
- Writeln('Количество операций присваивания: ', AssignmensCount);
- Writeln('Количество вычислительных операций: ', CalculationsCount);
- Writeln('ИТОГО: ', CalculationsCount+AssignmensCount+ComparisonsCount);
- End;
- Procedure SimpleBubbleSortWithFlag;
- Var
- A: TArr;
- I, J, Temp: Integer;
- IsSorted: Boolean;
- ComparisonsCount, AssignmensCount, CalculationsCount: Integer;
- Begin
- A := Copy(NumArr);
- ComparisonsCount := 0; // сравнения
- AssignmensCount := 0; // присваивания
- CalculationsCount := 0; // вычисления
- For J := Low(A) to High(A)-1 do
- Begin
- IsSorted := True;
- Inc(AssignmensCount);
- Inc(AssignmensCount);
- Inc(CalculationsCount);
- Inc(ComparisonsCount);
- For I := Low(A)+1 to High(A)-J do
- Begin
- Inc(AssignmensCount);
- Inc(CalculationsCount);
- if A[I-1] > A[I] then
- Begin
- Temp := A[I];
- A[I] := A[I-1];
- A[I-1] := Temp;
- IsSorted := False;
- Inc(AssignmensCount, 4);
- End;
- Inc(ComparisonsCount);
- End;
- Inc(ComparisonsCount);
- if IsSorted then break;
- End;
- Writeln('SimpleBubbleSortWithFlag:');
- //OutputArray(A);
- Writeln('Количество операций сравнения: ', ComparisonsCount);
- Writeln('Количество операций присваивания: ', AssignmensCount);
- Writeln('Количество вычислительных операций: ', CalculationsCount);
- Writeln('ИТОГО: ', CalculationsCount+AssignmensCount+ComparisonsCount);
- End;
- Procedure SimpleShakerSort;
- Var
- A: TArr;
- I, J, Temp, Left, Right: Integer;
- ComparisonsCount, AssignmensCount, CalculationsCount: Integer;
- Begin
- A := Copy(NumArr);
- ComparisonsCount := 0; // сравнения
- AssignmensCount := 0; // присваивания
- CalculationsCount := 0; // вычисления
- Left := Low(A)+1;
- Right := High(A);
- Inc(AssignmensCount,2);
- while Left < Right do
- Begin
- Inc(ComparisonsCount);
- for I := Left to Right do
- Begin
- Inc(AssignmensCount);
- Inc(CalculationsCount);
- Inc(ComparisonsCount);
- if A[I-1] > A[I] then
- Begin
- Temp := A[I];
- A[I] := A[I-1];
- A[I-1] := Temp;
- Inc(AssignmensCount, 3);
- End;
- Inc(ComparisonsCount);
- End;
- Dec(Right);
- Inc(CalculationsCount);
- Inc(AssignmensCount);
- for I := Right Downto Left do
- Begin
- Inc(AssignmensCount);
- Inc(CalculationsCount);
- Inc(ComparisonsCount);
- if A[I-1] > A[I] then
- Begin
- Temp := A[I];
- A[I] := A[I-1];
- A[I-1] := Temp;
- Inc(AssignmensCount, 3);
- End;
- Inc(ComparisonsCount);
- End;
- Inc(Left);
- Inc(CalculationsCount);
- Inc(AssignmensCount);
- End;
- Writeln('SimpleShakerSort:');
- //OutputArray(A);
- Writeln('Количество операций сравнения: ', ComparisonsCount);
- Writeln('Количество операций присваивания: ', AssignmensCount);
- Writeln('Количество вычислительных операций: ', CalculationsCount);
- Writeln('ИТОГО: ', CalculationsCount+AssignmensCount+ComparisonsCount);
- End;
- Procedure ShakerSortWithFlag;
- Var
- A: TArr;
- I, J, Temp, Left, Right: Integer;
- ComparisonsCount, AssignmensCount, CalculationsCount: Integer;
- IsSorted: Boolean;
- Begin
- A := Copy(NumArr);
- ComparisonsCount := 0; // сравнения
- AssignmensCount := 0; // присваивания
- CalculationsCount := 0; // вычисления
- Left := Low(A)+1;
- Right := High(A);
- Inc(AssignmensCount,2);
- while (Left < Right) And Not IsSorted do
- Begin
- Inc(ComparisonsCount, 2);
- IsSorted := True;
- Inc(AssignmensCount);
- for I := Left to Right do
- Begin
- Inc(AssignmensCount);
- Inc(CalculationsCount);
- Inc(ComparisonsCount);
- if A[I-1] > A[I] then
- Begin
- Temp := A[I];
- A[I] := A[I-1];
- A[I-1] := Temp;
- IsSorted := False;
- Inc(AssignmensCount, 4);
- End;
- Inc(ComparisonsCount);
- End;
- Dec(Right);
- Inc(CalculationsCount);
- Inc(AssignmensCount);
- for I := Right Downto Left do
- Begin
- Inc(AssignmensCount);
- Inc(CalculationsCount);
- Inc(ComparisonsCount);
- if A[I-1] > A[I] then
- Begin
- Temp := A[I];
- A[I] := A[I-1];
- A[I-1] := Temp;
- IsSorted := False;
- Inc(AssignmensCount, 4);
- End;
- Inc(ComparisonsCount);
- End;
- Inc(Left);
- Inc(CalculationsCount);
- Inc(AssignmensCount);
- End;
- Writeln('ShakerSortWithFlag:');
- //OutputArray(A);
- Writeln('Количество операций сравнения: ', ComparisonsCount);
- Writeln('Количество операций присваивания: ', AssignmensCount);
- Writeln('Количество вычислительных операций: ', CalculationsCount);
- Writeln('ИТОГО: ', CalculationsCount+AssignmensCount+ComparisonsCount);
- End;
- begin
- Randomize;
- Writeln('Введите N');
- Readln(N);
- //Writeln('Исходный массив:');
- NumArr := CreateArr(N);
- //OutputArray(NumArr);
- //Writeln;
- SimpleBubbleSort;
- Writeln;
- SimpleBubbleSortWithFlag;
- Writeln;
- SimpleShakerSort;
- Writeln;
- ShakerSortWithFlag;
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement