kiem dinh pahn mem

TINH TONG

program tinh tong

var n,s,i,x:longint;

a:array[1..1000] of longint;

begin

s:=0;

x:=0;

writeln('nhap so phan tu cua mang ');readln(n);

for i:=1 to n do

write('nhap phan tu thu ',i,':');readln(a[i]);

max:=a[1];

for i:=1 to n do

begin

if (a[i] mod 2) =0 then s:=s+a[i];

if (a[i] mod 2)=1 then x:=x+a[i];

end;

writeln('tong cac so chan la:',s);

write('tong cac so le la:',x);

readln

end.

TIM KIEM TUAN TU

Type diem = record

key, infor: integer;

end;

var A: array[1..maxN] of diem;

N: integer;

procedure intialze;

begin N:=0;

end;

function seqsearch(v: integer; x : integer): integer;

begin

A[N+1].key:=v;

If x then

repeat x:= x+ 1

until v = A[x].key;

seqsearch:= x;

end;

end;

function seqinsert(v : integer): integer;

begin

N:= N + 1;

A[N].key:= v;

seqinsert := N;

end

end;

TIM KIEM TUAN TU BANG XAU CO THU TU

type lienket = ↑ diem;

diem = record

key, infor :integer;

next : lienket;

end;

var dau , t , x : lienket;

i: integer;

procedure initialize;

begin

new(z);

z↑.next:= z;

new(dau);

dau↑.next:= z ;

end;

function listsearch(v : integer; t:lienket) : lienket;

begin

z↑.key:= v;

repeat

t:= t↑.next

until v < = t↑.key;

if (v= t↑.key) then

listseach:= t;

else

listseach:= z;

end;

end;

TIM KIEM TUAN TU NHI PHAN

function TKnhiphan(k: integer): integer;

var x, l , r : integer;

begin

l:=1;

r:= N;

repeat

x:= (l + r ) div 2;

if (kA[x].key) then

r:= x - 1;

else

r:= x + 1;

until (k = A[x].k) or (l>r);

if (k= A[x].key) then

TKnhiphan:= x;

else TKnhiphan:= N +1;

end;

SAP XEP BANG THUAT TOAN QUICK SORT

function quickSort(A, lower, upper){

        x = A[(lower + upper) / 2];

        i = lower;

        j = upper;

        do{

                while(A[i] < x)

                        i ++;

                while (A[j] > x)

                        j --;

                if (i <= j){

                        swap(A[i], A[j]);

                        i ++;

                        j --;

                }

        }while(i <= j);

        if (j > lower)

                quickSort(A, lower, j);

        if (i < upper)

                quickSort(A, i, upper);

}

Độ phức tạp tính toán: O(nlnn)

GIAI THUAT THUAT TOAN QUICK SORT

Giai thuat: QuickSort(nodes [ ], low, up)

Mo Ta; Giair thuat QuickSort, dung phuong phap de qui sawp xep va cac nut trong danh sach giua hai vi tri

  low va up

Du Lieu nhap:

 - Danh sach cac nut chua sap xep (giua hai vi tri low va up)

        - low va up

Du Lieu Xuat:

 Danh sach cac nut (giua hai vi tri low va up) da duoc sap xep

Hanh dong

 if(low >= up) // dieu kien dung

    ket thuc giai thuat

   if(low < up)

    - Phan hoach: partition(nodes[], low, up, pivot)

       + partition phan danh sach thanh ba phan:

            * nut lam truc: nodes[low] tro thanh nodes[pivot]

            * danh sach con 1: nodes[i] <= nodes[pivot]

             (voi i < pivot)

            * danh sach con 2: nodes[i] > nodes[pivot]

             (voi i > pivot)

      - Goi de qui: QuickSort(nodes[], low, pivot-1)

      - Goi de qui: QuickSort(nodes[], pivot+1, up)

Ket Thuc

CODE C++

void QuickSort(int nodes[], int low, int up)

{

   int pivot;

   if(low >= up)   // dieu kien dung

    return;

   if(low < up)

   {

        partition(nodes, low, up, &pivot);

       QuickSort(nodes, low, pivot - 1);

       QuickSort(nodes, pivot + 1, up);

   }

}

SAP XEP BANG THUAT TOAN DOI CHO

Dữ liệu nhập: Danh sách n nút chưa sắp xếp.

Dữ liệu xuất: Danh sách n nút đã sắp xếp.

Biến tạm: doicho

Hành động:

Gán doicho = true;

for(int i = 1;i < n && doicho = true;i++)

 {

      Gán doicho = flase;

      for(j = 0;i < n-1; j++)

          if(nodes[j] > nodes[j+1]))

          {

                Gán doicho = true; 

                Đỗi chỗ cho hai nút tại vị trí j và j+1

          }

 }

Kết thúc

C++ Code:Lựa chọn code | Ẩn/Hiện code

Dữ liệu nhập: Danh sách n nút chưa sắp xếp.

Dữ liệu xuất: Danh sách n nút đã sắp xếp.

Biến tạm: doicho

Hành động:

Gán doicho = true;

for(int i = 1;i < n && doicho = true;i++)

 {

      Gán doicho = flase;

      for(j = 0;i < n-1; j++)

          if(nodes[j] > nodes[j+1]))

          {

                Gán doicho = true; 

                Đỗi chỗ cho hai nút tại vị trí j và j+1

          }

 }

Kết thúc

GIAT THUAT SAP XEP THEO THUAT TOAN DOI CHO (BubbleSort) (C)

void BubbleSort(int nodes[],int n)  // n la so phan tu trong mang nodes[]

{

    int temp,i,j;

   bool doicho = true;

   for(i = 1; i < n && doicho == true; i++)

   {

    doicho = false;

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

            if(nodes[j] < nodes[j+1])

        {

            doicho = true;

            temp = nodes[j];

            nodes[j] = nodes[j+1];

            nodes[j+1] = temp;

        }

   }

}

Mô Tả Giải Thuật Bubble Sort

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: