You are on page 1of 4

data structures PART A Answer all the questions ( 5 X 2 = 10 ) 1) What is the major difference between linear search &

; binary search. Write th eir time complexities. Linear Search Binary Search 1. The list may be in any order 1. The list must be in ascending order. 2. Search the list in sequential manner 2. if the middle element = x(the elemen t to be searched), the element is found. If a[mid]<x, search in the left half of the list. If a[mid]>x, search in the right half of the list. 3. Computing time = O(n) 3. Computing time = O(log n) which is less than linear search. 2) Define Binary search tree with example. A binary search tree is a binary tree. It may be empty. If it is not empty then, it satisfies the following properties. 1. Every element has a key & the keys are distinct. 2. The keys in the left sub tree is smaller than the key in the root. 3. Keys in the right sub tree is larger than the key in the root. 4. The left & right sub trees are also BST. 3) What is the time complexity of bubble sort and merge sort? Time complexity of Bubble sort = O(n2) Time complexity of Merge sort = O(nlogn) 4) Differentiate between internal search and external search. Internal Search: If all the searching elements are present in the main memory itself then it is called as internal search. External Search: If the searching elements are also available in the secondary storage, t hen it is called as external search. 5) Sort the following numbers using binary tree sort. 8, 21, 31, 5, 4, 91, 54 PART B Draw Binary search tree for the given numbers Traverse in in-order. Sorted Elements: 4,5,8,21,31,54,91 (2 X 16 +1 X 8 = 40)

6 a) Explain the various operations on binary search tree with algorithms. (1 6) BST insertion: When adding a new node to a binary search tree, note that the new node will alwa ys be a leaf in the tree. To insert a new node, all we will do is navigate the B ST starting from the root. If the new node value is smaller than the current nod e value, we go left if it is larger, we go right. When we reach a leaf node, the last step is to attach the new node as a child to this leaf node in a way that preserves the BST constraint. BST Deletion: Deleting a node from BST is a little more subtle. Formally there are three cases for deleting node n from a BST:

If n has no children, we only have to remove n from the tree. If n has a single child, we remove n and connect its parent to its child. If n has two children, we need to : Find the smallest node that is larger than n, call it m. Remove m from the tree (if you have reached this case then m will always have no left child, though I'll leave the proof to the reader), so we apply one or the other of the above cases to do this. Replace the value of n with m. BST Retrieval: Retrieving an element from binary search trees requires simple navigation, start ing from the root and going left, if the current node is larger than the node we are looking for, or going right otherwise. (OR) 6 b) Explain the merge sort algorithm. Sort the following numbers using merge so rt 55, 25, 105, 3, 58, 307, 44, 556, 9, 35 (16) If (low<high) Find mid position. Mid = (low+high)/2 Call mergesort function for the list from low to mid. Call mergesort function for the list from mid+1 to high. Merge the 2 sorted list into a single sorted list Give an example and explain. Divide the array into 2 halves till a single elemen t is in the list. Merge the sorted arrays one by one to get a single sorted arra y. 3,9,25,35,44,55,58,105,307,556 7 a) i) Write the algorithm for Quick sort and explain (8) The QUICKSORT algorithm. //Sort an array A of n elements. Create global integer array A[1:n+1]; //The elements to be sorted are in A[1].A[n] //Step 1: Put a very large element in A[n+1]; //Find maximum of the n elements. //Store a large element at the end of the sequence in A. void max(1,n) { int max; max = A[1]; for (int I=2; I<=n; I++} if (max < A[I]) max = A[I]; A[n+1] = ++max; return(0); } //The Quicksort algorithm void Quicksort(int left, int right) { if (left >= right) return(0); int pivot = Partition(left, right + 1); Quicksort(left, pivot 1); Quicksort(pivot + 1,right); return(0); } //Partititon returns position of the pivot element //The pivot element is assumes to be the leftmost one int Partititon(int left, int right) { int pivot_element = A[left]; int left_to_right = ++left;

int right_to_left = --right; while(A[left_to_right] < pivot_element) ++left_to_right; while(A[right_to_left] > pivot_element) ++right_to_left; //posititon for pivot element is at right_to_left A[left] = A[right_to_left], A[right_to_left] = pivot_element; return (right_to_left); } ii) Sort the following list using quick sort. Tim, Dot, Era, Roy, Gom, Kim, Boy, Guy Ans: Boy,Dot,Era,Kim, Gom, Guy,Roy, Tim (OR) 7 b) Explain the various searching techniques with suitable examples. (8)

(16)

Binary Search: int binarysearch(int a[], int x) { While (low<high) { Mid = (low+high)/2; If (x==a[mid]) return(mid); else if (x<a[mid]) high=mid-1; else low=mid+!; } return(-1); } Interpolation search: For Interpolation search: Mid = low + [ (high low) * (key x[low]) (x[high] x[low]) Linear Search: 1. The list may be in any order 2. Search the list in sequential manner 3. Computing time = O(n) Indexed Sequential Search: Make use of an index called primary index or secondary index to search an elemen t. 8a) Sort the following numbers using heap sort. (8) 51, 24, 60, 89, 56, 38, 70 Form the max heap. Interchange first and last element and remove the last element. Again adjust the first node. Ans: 24,38,51,56,60,70,89 (OR) b) Explain the radix sort algorithm for sorting numbers (8) The bin sorting approach can be generalised in a technique that is known as radi x sorting. An example Assume that we have n integers in the range (0,n2) to be sorted. (For a bin sort , m = n2, and we would have an O(n+m) = O(n2) algorithm.) Sort them in two phase s: 1. Using n bins, place ai into bin ai mod n,

2. Repeat the process using n bins, placing ai into bin floor(ai/n), being careful to append to the end of each bin.

You might also like