Lecture Data Structures & Algorithms: Chapter 3
Số trang: 16
Loại file: pptx
Dung lượng: 626.25 KB
Lượt xem: 13
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Lecture Data Structures & Algorithms: Chapter 3 (Searching Techniques prens) presented linear (sequential) search, binary search, complexity of algorithms.
Nội dung trích xuất từ tài liệu:
Lecture Data Structures & Algorithms: Chapter 3 DONG NAI UNIVERSITY OF TECHNOLOGYData Structures & Algorithms 1 DONG NAI UNIVERSITY OF TECHNOLOGY SearchingTechniques 2 DONG NAI UNIVERSITY OF TECHNOLOGYLINEAR (SEQUENTIAL) SEARCH BINARY SEARCHCOMPLEXITY OF ALGORITHMS 3 DONG NAI UNIVERSITY OF TECHNOLOGY• To finding out whether a particular element is present in the list.• 2 methods: linear search, binary search• The method we use depends on how the elements of the list are organized – unordered list: • linear search: simple, slow – an ordered list: • binary search or linear search: complex, faster 4 DONG NAI UNIVERSITY OF TECHNOLOGY LINEAR (SEQUENTIAL) SEARCH• How? – Proceeds by sequentially comparing the key with elements in the list – Continues until either we find a match or the end of the list is encountered. – If we find a match, the search terminates successfully by returning the index of the element – If the end of the list is encountered without a match, the search terminates unsuccessfully. 5 DONG NAI UNIVERSITY OF TECHNOLOGYLINEAR (SEQUENTIAL) SEARCHint lsearch(int arr[], int n, int value){ for(int i=0;i DONG NAI UNIVERSITY OF TECHNOLOGY BINARY SEARCH• List must be a sorted one• We compare the element with the element placed approximately in the middle of the list• If a match is found, the search terminates successfully.• Otherwise, we continue the search for the key in a similar manner either in the upper half or the lower half. 7DONG NAI UNIVERSITY OF TECHNOLOGY 8void bsearch(int arr[],int n,int value) NAI UNIVERSITY OF TECHNOLOGY DONG{ int low,up,mid; low = 0; up = n-1; while(low By Recursion DONG NAI UNIVERSITY OF TECHNOLOGYint Search (int arr[], int value, int low, intup) { if (low DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• In Computer Science, it is important to measure the quality of algorithms, especially the specific amount of a certain resource an algorithm needs• Resources: time or memory storage (PDA?)• Different algorithms do same task with a different set of instructions in less or more time, space or effort than other.• The analysis has a strong mathematical background.• The most common way of qualifying an algorithm is the Asymptotic Notation, also 11 DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• It is generally written as• Polynomial time algorithms, – O(1) --- Constant time --- the time does not change in response to the size of the problem. – O(n) --- Linear time --- the time grows linearly with the size (n) of the problem. – O(n2) --- Quadratic time --- the time grows quadratically with the size (n) of the problem. In big O notation, all polynomials with the same degree are equivalent, so O(3n2 + 3n + 7) = O(n2)• Sub-linear time algorithms – O(logn) -- Logarithmic time• Super-polynomial time algorithms O(n!) ; O(2n) 12 DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• Example1: complexity of an algorithmvoid func ( int a[], int n ){ 2 * O(1) + O(N) cout DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• Example2: complexity of an algorithmvoid func ( int a[], int n ){ cout DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• Linear Search – O(n).• Binary Search – O(log2 N) 15 DONG NAI UNIVERSITY OF TECHNOLOGYEND 16
Nội dung trích xuất từ tài liệu:
Lecture Data Structures & Algorithms: Chapter 3 DONG NAI UNIVERSITY OF TECHNOLOGYData Structures & Algorithms 1 DONG NAI UNIVERSITY OF TECHNOLOGY SearchingTechniques 2 DONG NAI UNIVERSITY OF TECHNOLOGYLINEAR (SEQUENTIAL) SEARCH BINARY SEARCHCOMPLEXITY OF ALGORITHMS 3 DONG NAI UNIVERSITY OF TECHNOLOGY• To finding out whether a particular element is present in the list.• 2 methods: linear search, binary search• The method we use depends on how the elements of the list are organized – unordered list: • linear search: simple, slow – an ordered list: • binary search or linear search: complex, faster 4 DONG NAI UNIVERSITY OF TECHNOLOGY LINEAR (SEQUENTIAL) SEARCH• How? – Proceeds by sequentially comparing the key with elements in the list – Continues until either we find a match or the end of the list is encountered. – If we find a match, the search terminates successfully by returning the index of the element – If the end of the list is encountered without a match, the search terminates unsuccessfully. 5 DONG NAI UNIVERSITY OF TECHNOLOGYLINEAR (SEQUENTIAL) SEARCHint lsearch(int arr[], int n, int value){ for(int i=0;i DONG NAI UNIVERSITY OF TECHNOLOGY BINARY SEARCH• List must be a sorted one• We compare the element with the element placed approximately in the middle of the list• If a match is found, the search terminates successfully.• Otherwise, we continue the search for the key in a similar manner either in the upper half or the lower half. 7DONG NAI UNIVERSITY OF TECHNOLOGY 8void bsearch(int arr[],int n,int value) NAI UNIVERSITY OF TECHNOLOGY DONG{ int low,up,mid; low = 0; up = n-1; while(low By Recursion DONG NAI UNIVERSITY OF TECHNOLOGYint Search (int arr[], int value, int low, intup) { if (low DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• In Computer Science, it is important to measure the quality of algorithms, especially the specific amount of a certain resource an algorithm needs• Resources: time or memory storage (PDA?)• Different algorithms do same task with a different set of instructions in less or more time, space or effort than other.• The analysis has a strong mathematical background.• The most common way of qualifying an algorithm is the Asymptotic Notation, also 11 DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• It is generally written as• Polynomial time algorithms, – O(1) --- Constant time --- the time does not change in response to the size of the problem. – O(n) --- Linear time --- the time grows linearly with the size (n) of the problem. – O(n2) --- Quadratic time --- the time grows quadratically with the size (n) of the problem. In big O notation, all polynomials with the same degree are equivalent, so O(3n2 + 3n + 7) = O(n2)• Sub-linear time algorithms – O(logn) -- Logarithmic time• Super-polynomial time algorithms O(n!) ; O(2n) 12 DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• Example1: complexity of an algorithmvoid func ( int a[], int n ){ 2 * O(1) + O(N) cout DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• Example2: complexity of an algorithmvoid func ( int a[], int n ){ cout DONG NAI UNIVERSITY OF TECHNOLOGY COMPLEXITY OF ALGORITHMS• Linear Search – O(n).• Binary Search – O(log2 N) 15 DONG NAI UNIVERSITY OF TECHNOLOGYEND 16
Tìm kiếm theo từ khóa liên quan:
Cấu trúc dữ liệu Bài giảng Cấu trúc dữ liệu và giải thuật Cơ sở dữ liệu Lecture Data Structures Data Algorithms Công nghệ thông tinTài liệu liên quan:
-
52 trang 432 1 0
-
62 trang 403 3 0
-
Đề thi kết thúc học phần học kì 2 môn Cơ sở dữ liệu năm 2019-2020 có đáp án - Trường ĐH Đồng Tháp
5 trang 378 6 0 -
Đề cương chi tiết học phần Cấu trúc dữ liệu và giải thuật (Data structures and algorithms)
10 trang 320 0 0 -
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 319 0 0 -
74 trang 303 0 0
-
13 trang 298 0 0
-
96 trang 297 0 0
-
Giáo trình Cơ sở dữ liệu: Phần 2 - TS. Nguyễn Hoàng Sơn
158 trang 296 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0