TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘIVIỆN CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG Cấu trúc dữ liệu và giải thuật Nguyễn Khánh Phương Computer Science department School of Information and Communica
Trang 1TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI
VIỆN CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
Cấu trúc dữ liệu và giải thuật
Nguyễn Khánh Phương
Computer Science department School of Information and Communication technologycuu duong than cong com
Trang 2Nội dung khóa học
Chương 1 Các kiến thức cơ bản
Chương 2 Thuật toán đệ quy
Chương 3 Các cấu trúc dữ liệu cơ bản
Chương 4 Cây
Chương 5 Sắp xếp
Chương 6 Tìm kiếm
Chương 7 Cấu trúc dữ liệu đồ thị cuu duong than cong com
Trang 3TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI VIỆN CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
Chương 6 Tìm kiếm (Searching)
Nguyễn Khánh Phương
Computer Science department School of Information and Communication technologycuu duong than cong com
Trang 4Bài toán tìm kiếm (Searching problem)
Cho danh sách A gồm n phần tử a1, a2, , an và 1 số x
Câu hỏi: x có mặt trong danh sách A hay không?
• Nếu x có mặt trong danh sách A, hãy đưa ra vị trí xuất hiện của x trong danh sách đã cho, nghĩa là đưa ra chỉ số i sao cho ai = x
cuu duong than cong com
Trang 5N i dung
1 Tìm kiếm tuần tự
2 Tìm kiếm nhị phân
3 Cây nhị phân tìm kiếm
4 Cây AVL
5 Bảng băm
cuu duong than cong com
Trang 6N i dung
1 Tìm kiếm tuần tự
2 Tìm kiếm nhị phân
3 Cây nhị phân tìm kiếm
4 Cây AVL
5 Bảng băm
cuu duong than cong com
Trang 71 Tìm kiếm tuần tự (Linear Search/Sequential search)
• Đầu vào:
– Cho mảng A gồm n phần tử và giá trị tìm kiếm x.
– Mảng A không cần thiết đã được sắp xếp
• Thuật toán: Bắt đầu từ phần tử đầu tiên, duyệt qua từng phần tử cho đến khi tìm được x hoặc toàn bộ các phần tử của mảng đã được duyệt hết
• Độ phức tạp: O(n)
A:
Target x = 8: cuu duong than cong com
Trang 81 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=1
cuu duong than cong com
Trang 91 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=2
cuu duong than cong com
Trang 101 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=3
cuu duong than cong com
Trang 111 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=4
cuu duong than cong com
Trang 121 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=5
cuu duong than cong com
Trang 131 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i);
else
printf(“The target is NOT in the list”);
}
Array A
i=5
cuu duong than cong com
Trang 141 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=1
cuu duong than cong com
Trang 151 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=2
cuu duong than cong com
Trang 161 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=3
cuu duong than cong com
Trang 171 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=4
cuu duong than cong com
Trang 181 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=5
cuu duong than cong com
Trang 191 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i); else
printf(“The target is NOT in the list”);
}
Array A
i=6
cuu duong than cong com
Trang 201 Tìm kiếm tuần tự (Linear Search/Sequential search)
void linearSearch(int a[], int size, int target)
{ int i;
for (i = 1; i <= size; i++)
if (a[i] == target) break;
if (i <= size)
printf(“The target is in the list @ index = %d”,i);
else
printf(“The target is NOT in the list”);
}
Array A
i=7
cuu duong than cong com