cấu trúc dữ liệu giải thuật

Post on 07-Feb-2016

74 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Cấu Trúc Dữ Liệu Giải Thuật. YNT HoàNT. Giải thuật Giải thuật sắp xếp Giải thuật tìm kiếm. Day 1. Đơn vị cấu trúc. Cấu trúc dữ liệu. Bài toán Lựa chọn GT. Giải thuật. Bài toán Lựa chọn GT. Giải thuật. Selection Sort Bubble Sort Quick Sort. Sắp xếp (sorting). - PowerPoint PPT Presentation

TRANSCRIPT

Cấu Trúc Dữ Liệu

Giải ThuậtYNT

HoàNT

Giải thuậtGiải thuật sắp xếpGiải thuật tìm kiếm

Day 1

Đơn vị cấu trúc

Cấu trúc dữ liệu

Bài toánLựa chọn GT

Giải thuật

Bài toánLựa chọn GT

Giải thuật

Selection SortBubble SortQuick Sort

Sắp xếp (sorting)

Selection Sortselectionsort(data[])

for i = 0 to data.length-2select the smallest element among

data[i] to data[data.length-1];swap it with data[i];

Selection Sort

Bubble Sortpublic void bubblesort(int[] data) {

for (int i = 0; i < data.length-1; i++)

for (int j = data.length-1; j > i; --j)

if (data[j] < data[j-1])

swap(data,j,j-1);

Bubble Sort

Quick Sortvoid Partition(int *k, int L, int H){

int i,j;

int pivot;

if (L >= H) return;

pivot = k[(L+H)/2];

i = L;

j = H;

Link

do{

while (k[i] < pivot) i++;

while (k[j] > pivot) j--;

if (i<=j){

swap(k,i,j);

i++;

j--;

}

}

while (i >= j);

Partition(L,j);

Partition(i,H);

}

Tìm kiếm

For each item in the list: if that item has the desired value, return the item's location.

Return Λ.

Tuyế�n tính : Linear Search

Tìm kiếm

while (first <= last) { int mid = (first + last) / 2; // compute mid point. if (key > sortedArray[mid]) first = mid + 1; else if (key < sortedArray[mid]) last = mid - 1;

else return mid; // found it. return position ///// } return -1;

Nhị phân: Binary Search

Bài toán

In ra màn hình, tất cả các số nguyên có 4 chữ số được tạo từ các số 1,2,3,4

Vét cạnmethod Try(i)

For (mọI giá trị V có thể gán cho x[i])thử cho x[i] = Vif (x[i] là phần tử cuối cung)

<thông báo kết quả vừa tìm đc>Else

Ghi nhận x[i] đã nhận gtri V //optionalTry(i+1) // gọi đệ quy chọn tiếp x[i+1]Bỏ Ghi nhận x[i] đã nhận gtri V

//optionalEnd For

end method

Đệ quy + quay lui (Vét cạn)

Áp dụng vào bài toán.

DAY 3• BFS

• DFS

ĐỒ THỊ

ĐỒ THỊ

BIỂU DIỄN ĐỒ THỊ• Ma trận kề (Adjacency matrix)

• Danh sách cạnh (Edge list)

• Danh sách kề (Adjacency List)

Ma trận kề (Adjacency matrix)

Danh sách cạnh (Edge list)

Danh sách kề (Adjacency List)

TÌM KIẾM TRÊN ĐỒ THỊ

• Depth First Search

• Breadth First Search

Depth First Search

Breadth First Search

Breadth First Search

Breadth First Search1 procedure BFS(G,v):2 create a queue Q3 enqueue v onto Q4 mark v5 while Q is not empty:6 t ← Q.dequeue()

for all vertex near t do11 if o is not marked:12 mark o13 enqueue o onto Q

top related