iİnformatikanın məktəbdə tədrisi

8-8r

Дан массив. Написать программу упорядочения массива методом просеивания.




1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

Program A8_08a;

Const

  n=12;

  a:array[0..n-1] of integer=

   (1,6,-4,9,-3,8,5,7,3,-8,19,-3);

Var  i:integer;

 

Procedure sortpros(Var x:array of integer;nx:integer);

 Var i,j,c:integer;

begin

 for i:=0 to nx-2 do

  if (x[i]>x[i+1]) then

   for j:=i+1 downto 1 do

    if (x[j]<x[j-1]) then

      begin   c:=x[j];

       x[j]:=x[j-1]; x[j-1]:=c;

      end;

end;

BEGIN

 for i:=0 to n-1 do

   write(a[i],' ');

 writeln;

 sortpros(a,n);

 for i:=0 to n-1 do

   write(a[i],' ');

 writeln;

 readln;

END.

//Program A8_08a;

#include <iostream>

 

using namespace std;

 

void sortpros(int *x, int nx)

 { int j,i,c;

   for (i=0;i<nx-1;i++)

     if (x[i]>x[i+1])

         for(j=i+1;j>0 && x[j]<x[j-1];j--)

           {c=x[j];x[j]=x[j-1];x[j-1]=c; }

  }

int main()

{  int a[]={ 1,6,-4,9,-3,8,5,7,3,-8,19,-3};

    int n=sizeof(a)/sizeof(int);

    int i;

    for(i=0; i<n; i++)

        cout<<a[i]<<" ";  cout<<endl;

   sortpros(a,n);

     for(i=0; i<n; i++)

        cout<<a[i]<<" ";  cout<<endl;

    return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

Program A8_08b;

Var

  fin,fout: TEXT;

  a:array of integer;

  i,n:integer;

 Procedure sortpros(Var x:array of integer;nx:integer);

 Var i,j,c:integer;

begin

 for i:=0 to nx-2 do

  if (x[i]>x[i+1]) then

   for j:=i+1 downto 1 do

    if (x[j]<x[j-1]) then

      begin   c:=x[j];

       x[j]:=x[j-1]; x[j-1]:=c;

      end;

end;

BEGIN

 assign(fin,'in8_08b.txt'); reset(fin);

 if IoResult<>0 then

   begin writeln('Net faila');

   readln; exit; end;

 readln(fin,n);

 setLength(a,n);

 for i:=0 to n-1 do

  readln(fin,a[i]);

  close(fin);

  sortpros(a,n);

 assign(fout,'out8_08b.txt'); rewrite(fout);

 writeln(fout,n);

 for i:=0 to n-1 do

   writeln(fout,a[i]);

 Finalize(a);

close(fout);

END.

//Program A8_08b;

#include <iostream>

#include <fstream>

 

using namespace std;

 

void sortpros(int *x, int nx)

 { int j,i,c;

   for (i=0;i<nx-1;i++)

     if (x[i]>x[i+1])

         for(j=i+1;j>0 && x[j]<x[j-1];j--)

           {c=x[j];x[j]=x[j-1];x[j-1]=c; }

  }

int main()

{ int  *a;  int n,i;

    ifstream fin;

    fin.open("in8_08b.txt");

    if (!fin) { cout<<"net faila\n"; return -1;}

   fin>>n;

   a=new int [n];

   for(i=0; i<n; i++)

        fin>>a[i];

    fin.close();

    sortpros(a,n);

    ofstream fout;

  fout.open("out8_08b.txt");

    fout<<n<<endl;

     for(i=0; i<n; i++)

        fout<<a[i]<<endl;

     delete [] a;

      fout.close();

    return 0;

}