Danh mục

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    
tailieu_vip

Hỗ trợ phí lưu trữ khi tải xuống: 10,000 VND Tải xuống file đầy đủ (16 trang) 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

Tài liệu được xem nhiều:

Tài liệu liên quan: