Presentation is loading. Please wait.

Presentation is loading. Please wait.

1.1 Data Structure and Algorithm Lecture 1 Array Record Sequential Search Binary Search Bubble Sort Recursion Complexity Topics.

Similar presentations


Presentation on theme: "1.1 Data Structure and Algorithm Lecture 1 Array Record Sequential Search Binary Search Bubble Sort Recursion Complexity Topics."— Presentation transcript:

1 1.1 Data Structure and Algorithm Lecture 1 Array Record Sequential Search Binary Search Bubble Sort Recursion Complexity Topics

2 1.2 Data Structure and Algorithm Array An array is a sequence of elements. All the elements of an array must be of the same type (e.g. integer). Each elements can be selected by specifying its position. 5613417203 0123456789 index int list[10] Dhaka 01234 index char city[5]

3 1.3 Data Structure and Algorithm Record A record/structure is a collection of one or more variables, possibly of different types. An array is a sequence of elements all having the same type and size, whereas a record or structure is a sequence of elements having different types and sizes. Each element of a record is its member. Employee: Record name:char[20] address:char[50] salary: real struct employee{ char name[20]; char address[50]; float salary; }; Fig: C Convention

4 1.4 Data Structure and Algorithm Sequential Search [ Find the location where list array keeps the value of k ] sequential_search(k) returns integer Begin i = 0 while i k do i = i+1 if i<array_size then return i else return -1 End

5 1.5 Data Structure and Algorithm “C” implementation of Sequential Search #include int list[10] = {5,6,1,3,4,1,7,2,0,3}; int array_size = 10; int sequential_search(int k){ int i = 0; while ( (i<array_size) && ( list[i] != k) ) i++; if (i<array_size) return i; else return -1; } void main(){ int pos = sequential_search(2); printf(“The position is:%d”,pos); }

6 1.6 Data Structure and Algorithm Binary Search [ Find the location where sorted table array keeps the value of k ] binary_search(k): returns integer BEGIN l = 0 h = array_size found = 0; while l<h and found = 0 do m= (l+h)/2 //find the middle element xm = table[m] case xm<k: l = m+1 xm>k: h = m-1 xm=k: found = 1 if found then return m else return -1 END

7 1.7 Data Structure and Algorithm “C” Implementation of Binary Search #include int table[10] = {1,5,8,12,15,20,34,40,55,67}; int array_size = 10; int binary_search(int k){ int l =0; int h = array_size; int found =0,m,xm; while ( (l<h) && (found == 0) ){ m = (l+h)/2; xm = table[m]; if (xm<k) l = m+1; else if (xm>k) h = m-1; else found = 1; } if (found) return m; else return -1; } void main(){ int pos = binary_search(20); printf("The position is:%d",pos); }

8 1.8 Data Structure and Algorithm Recursive Binary Search [ Find the location where sorted table array keeps the value of k ] binary_search(l,h,k) BEGIN if l>h index = -1 else m= (l+h)/2 //find the middle element xm = table[m] case xm<k: binary_search(m+1,h) xm>k: binary_search(l,m-1) xm=k: index=m END

9 1.9 Data Structure and Algorithm Bubble Sort For i=0 to n-2 do BEGIN For j = i+1 to n-1 do BEGIN if list[i] > x[j] then swap list[i] and list[j] END

10 1.10 Data Structure and Algorithm Complexity Generally the complexity of an algorithm is measured by the two factors:  Time complexity  Space complexity Sequential search : O(n) Binary search: O(log 2 n) Bubble sort: O(n 2 )


Download ppt "1.1 Data Structure and Algorithm Lecture 1 Array Record Sequential Search Binary Search Bubble Sort Recursion Complexity Topics."

Similar presentations


Ads by Google