100 likes | 270 Views
Lecture 1. Array Record Sequential Search Binary Search Bubble Sort Recursion Complexity. Topics. 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. int list[10].
E N D
Lecture 1 • Array • Record • Sequential Search • Binary Search • Bubble Sort • Recursion • Complexity Topics 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. int list[10] index char city[5] index 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 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<array_size and list[i] <>k do • i = i+1 • if i<array_size then • return i • else • return -1 • End Data Structure and Algorithm
“C” implementation of Sequential Search • #include <stdio.h> • 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); • } 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 Data Structure and Algorithm
“C” Implementation of Binary Search • #include <stdio.h> • 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); • } 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 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 • END 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(log2n) • Bubble sort: O(n2) Data Structure and Algorithm