data structures and algorithm analysis-0108

18
Question Paper Data Structures and Algorithm Analysis (MC211): January 2008 Section A : Basic Concepts (30 Marks) This section consists of questions with serial number 1 - 30. Answer all questionst. Each question carries one mark. Maximum time for answering Section A is 30 Minutes. 1. Sparse matrices are represented using (a) Singly Linked List where each node contains non- zero elements of the matrix (b) Circular Linked List for each row, column with each node corresponding to non-zero element (c) Singly linked list where each node contains atleast one zero elements of the matrix (d) Can’t be represented using Linked List, but with arrays only (e) Doubly Linked List. 2. Inserting a node in between the given two nodes in the doubly linked list requires the change of the values for (a) Four pointers (b) Three pointers (c) Five pointers (d) Two pointers (e) One pointer. 3. In OBST for any values of i, j, w(i, j) is defined as (a) p(i) + q(i) + w(i+1, j-1) (b) p(i) + q(j) + w(i, j-1) (c) p(j) + q(j) + w(i-1, j) (d) p(j) + q(j) + w(i, j-1) (e) p(j) + q(i) + w(i-1, j-1). 4. Mark the one which is the odd one out the following (a) Minimum & Maximum problem (b) Merge sort (c) Knapsack problem (d) Selection problem (e) Quick sort.

Upload: amitukumar

Post on 22-Jul-2016

92 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Data Structures and Algorithm Analysis-0108

Question PaperData Structures and Algorithm Analysis (MC211): January 2008

Section A : Basic Concepts (30 Marks)

This section consists of questions with serial number 1 - 30.

Answer all questionst. Each question carries one mark. Maximum time for answering Section A is 30

Minutes.

1. Sparse matrices are represented using

(a) Singly Linked List where each node contains non- zero elements of the matrix (b) Circular Linked List for each row, column with each node corresponding to non-zero element(c) Singly linked list where each node contains atleast one zero elements of the matrix(d) Can’t be represented using Linked List, but with arrays only(e) Doubly Linked List.

2. Inserting a node in between the given two nodes in the doubly linked list requires the change of the values for

(a) Four pointers (b) Three pointers (c) Five pointers(d) Two pointers (e) One pointer.

3. In OBST for any values of i, j, w(i, j) is defined as

(a) p(i) + q(i) + w(i+1, j-1)(b) p(i) + q(j) + w(i, j-1)(c) p(j) + q(j) + w(i-1, j)(d) p(j) + q(j) + w(i, j-1)(e) p(j) + q(i) + w(i-1, j-1).

4. Mark the one which is the odd one out the following

(a) Minimum & Maximum problem(b) Merge sort(c) Knapsack problem(d) Selection problem(e) Quick sort.

5. For multistage graph cost (i, j) is calculated by following the forward approach

(a)i 1, j,l EC Vmin , {c(j,l) + cost(i+1, l)}

(b)i 1, j,l EC Vmin

{c(j+1,l) + cost(i+1, l+1)}

(c)i 1, j,l EC Vmin

{c(j+1,l) + cost(i+1, l)}

(d)i 1, j,l EC Vmin {c(j,l) + cost(i, l+1)}

(e)i 1, j,l EC Vmin {c(j+1,1+l) + cost(i, l+1)}.

i. exe

Page 2: Data Structures and Algorithm Analysis-0108

6. Choose the correct answer for the following set of statements

i. N2 ( N3)ii. 5N2 – 6N = (N2)(a) (II) is FALSE and (I) is TRUE(b) (I) is TRUE and (II) is TRUE(c) (I) is FALSE and (II) is TRUE(d) (I) is FALSE and (II) is FALSE(e) None of the above.

7. The maximum number of minimum cost spanning trees one can create for any given connected graph G = (N, V) is

(a) N(b) One(c) N + 1(d) 2NCN(e) N – 1.

8. Null pointer is used to define

(a) End of linked list(b) Empty pointer field of a structure(c) The linked list is empty(d) All (a), (b) and (c) above(e) Both (a) and (b) above.

9. The divided and conquer merge sort algorithm’s time complexity can be de direct or random access of element is not possible in

(a) Linked list(b) Array(c) String(d) Stacks(e) Queues.

10. The nth node, in singly linked list, is accessed via (where n>1)

(a) The head node(b) The tail node(c) (n-1) nodes(d) The nth node(e) The first node.

11. An Array is defined as

(a) Linear data structure(b) Non – linear data structure(c) Complex data structure(d) Not a data structure(e) A linked list.

12. A data structure, in which a element is added and removed only from only one end, is known as

(a) Queue(b) Stack(c) Array(d) Structures(e) Linked list.

13. In memory allocation, an entry which is smallest amongst all entries which equals to or is bigger than the required one is selected in

(a) First fit method(b) Best fit method(c) Worst fit method(d) Priority queue(e) None of the above.

14. In ‘C’, it is best to implement chained hash table as an array of

(a) Strings(b) Structures(c) Pointers(d) Classes(e) Linked list.

Page 3: Data Structures and Algorithm Analysis-0108

15. A B-tree of order n is also called

(a) (n-n) –1 tree(b) n-(n-2) tree(c) (n-1)-n tree(d) n-1 tree(e) n-2 tree.

16. atoi() is a C function which converts character strings to

(a) Equivalent floating point value(b) Equivalent integer value(c) Equivalent octal value(d) Equivalent hex value(e) Equivalent boolean value.

17. Which of the following statements is true in view of a multiway search tree, when a node has

(a) 4 subtrees it contains 3 keys(b) 5 keys, it has 7 subtrees(c) 6 subtrees, it contains 6 keys(d) 4 keys, it has 5 subtrees(e) 2 subtrees it contains 5 keys.

18. If a graph G = (N, E), where E= {f}, then the corresponding adjacency matrix is called as

(a) Unit Matrix(b) Identify Matrix(c) Having Diagonal elements Zero(d) Zero Matrix(e) Sparse Matrix.

19. Read the following statements carefully, and choose the correct answer

I. For the Backtracking algorithms stack data structure used.II. For the Branch-and-bound algorithms queue data structure used.

(a) (I) is FALSE but (II) is TRUE(b) Both (I) and (II) are TRUE(c) (I) is TRUE but (II) is FALSE(d) Both (I) and (II) are FALSE(e) None of these.

20. For the 15-puzzle problem if the arrangement is as follows, then the value of x used to find the reachability?

1 3 5 7

9 11 14 x

2 4 8 6

10 12 15 13

(a) 0(b) 1(c) 8(d) 14(e) 16.

21. Which method of traversal does not use stack to hold nodes that are waiting to be processed?

(a) Dept First(b) D-search(c) Breadth first(d) Back-tracking(e) Binary search.

22. f(n) (g(n)), where f, g : N –> Rfor all nN, defines f(n) in terms of g(n) for

(a) Middle bound(b) Upper bound (c) Lower bound(d) Both the upper & lower bounds (e) All (a), (b) and (c) above.

Page 4: Data Structures and Algorithm Analysis-0108

23. When the following function is called

void disp( node *first ){

if( first->next ){printf("%4d", first->data);disp( first->next );}

}

The output

(a) Will display the data of next node always in the list(b) Will not run at all(c) Will display the data of all nodes of the list(d) Will display the data of the first node only(e) Will display the data of the last node in the list.

24. A binary tree of depth d is almost complete binary tree, if

(a) Each leaf in the tree is either at level d or at level d-1(b) For any node n in the tree with a right descendant at level d, all the left descendants of n that are leaves

are also at level d(c) Both (a) and (b) are true(d) (a) true but not (b)(e) (b) true but not (a).

25. In two’s complement representation of an integer, if all the bits are 1, then the number is

(a) 1(b) -1(c) -32768(d) 32768(e) None of the above.

26. Stack is defined as

(a) Static data structure(b) Dynamic data structure(c) In built data structure(d) Differential equation(e) Complex data structure.

27. Representation of list structures with items that are either sublists or individual items, by a singly linked list is

(a) Possible with an additional tag field (storing information or address of the sublist in the information field of the node)

(b) Not possible(c) Possible without additional tag field(d) Not possible without additional link fields(e) Not possible with additional link fields.

28. The technique of Linear probing for collision resolution can lead to

(a) Clustering(b) Radix sort(c) Efficient storage utilization(d) Collision(e) Overflow.

29. A binary tree with the property that the key sorted at each node is greater than those stored in its children, is called

(a) B-tree(b) Heap(c) Binary Search tree(d) AVL tree(e) Quarternary tree.

Page 5: Data Structures and Algorithm Analysis-0108

30. This sort finds location LOC of smallest elements in A(K), …. , A(N) and then interchange A(LOC) with A(K) for K = 1, …., N – 1.

(a) Shuffle sort(b) Quick sort(c) Heap sort(d) Merge sort(e) Selection sort.

END OF SECTION A

Page 6: Data Structures and Algorithm Analysis-0108

Section B : Problems (50 Marks) This section consists of questions with serial number 1 – 5 . Answer all questions. Marks are indicated against each question. Detailed workings should form part of your answer. Do not spend more than 110 - 120 minutes on Section B.

1. a. Compare Quick sort with Merge sort for their efficiency. ( 5 marks)

b. What is a B-tree? Write about its advantage over the normal trees? ( 5 marks)

2. a. What is a circular linked list? Write about its ADT and advantages. ( 5 marks)

b. Write about spanning tree and list their applications. ( 5 marks)

3. a. Write an algorithm to color the graph using the branch and bound strategy. ( 5 marks)

b. What is meant by HAMILTONIAN GRAPH, write the algorithm to find the Hamiltonian cycles of the graph. ( 5 marks)

4. a. Write an algorithm to find the solution for N – Queen’s problem. ( 5 marks)

b. Write an algorithm for Knapsack problem following the Backtracking technique. ( 5 marks)

5. a. Write about the following

i. P – CLASS.ii. NP – HARD CLASS.iii. NP – COMPLETE. ( 6 marks)

b. Write the general analogy of Dynamic Programming. ( 4 marks)

END OF SECTION B

Section C : Applied Theory (20 Marks) This section consists of questions with serial number 6 - 7 . Answer all questions. Marks are indicated against each question. Do not spend more than 25 -30 minutes on Section C.

6. Write a ‘C’ program to create a linked list using recursive function. ( 10 marks)

7. Write a program to sort the element following Two – way merge sort technique. ( 10 marks)

END OF SECTION C

END OF QUESTION PAPER

Page 7: Data Structures and Algorithm Analysis-0108

Suggested AnswersData Structures and Algorithm Analysis (MC211): January 2008

Section A : Basic ConceptsAnswer Reason

1. A Singly Linked List where each node contains non- zero elements of the matrix.

2. A Four pointers.

3. B p(i) + q(j) + w(i, j-1).

4. C Knapsack problem.

5. Ai 1, j,l EC Vmin , {c(j,l) + cost(i+1, l)}.

6. C (I) is FALSE and (II) is TRUE.

7. B One.

8. D (a), (b) & (c) are correct.

9. C string.

10. C (n-1) nodes.

11. A linear data structure.

12. B stack.

13. B best fit method.

14. C pointers.

15. C (n-1)-n tree.

16. B equivalent integer value.

17. A 4 subtrees it contains 3 keys.

18. D Zero Matrix.

19. B (I), (II) both are TRUE.

20. A 0.

21. C Breadth first.

22. D Both the upper & lower bounds.

23. C Will display the data of all nodes of the list.

24. B For any node n in the tree with a right descendant at level d, all the left descendants of n that are leaves are also at level d.

25. B –1.

26. B dynamic data structure.

27. A possible with an additional tag field (storing information or address of the sublist in the information field of the node).

28. A Clustering.

29. B Heap.

30. A Shuffle sort.

Page 8: Data Structures and Algorithm Analysis-0108

Section B : Problems

1. a. Quicksort is the fastest sorting algorithm in practice but has a number of pathological cases that can make it perform as badly as O(n2). mergesort is guaranteed to run in O(n*ln(n)) but requires n units of additional temporary storageQuicksort vs. Merge Sort is a good example of an average-case vs. worst-case performance trade-off. Quicksort has excellent average-case time efficiency.Merits:(i) In all of the cases it takes nlogn time which is good when compared with Quick sort.(ii) If the list is small then it is good as it takes a small amount of auxiliary memory.Demerits:(i) Once the left and right parts are sorted, again then are merged into a single array, and hence the elements are going to

occupy different places before they appear in this final position (As contrarily to quick sort)(ii) It requires extra memory (Auxiliary memory to merge the parts), and hence wastage of memory if the length of the

array is very large.b. B-Tree is a multiway search tree of order m in which:

All leaves are on the bottom level.All internal nodes (except perhaps the root node) have at least m/2 (nonempty) children. The root node can have as few as 2 children if it is an internal node, and can obviously have no children if the root node is a leaf (that is, the whole tree consists only of the root node). Each leaf node (other than the root node if it is a leaf) must contain at least (m/2) - 1 keys. The following is an example of a B-tree of order 3. This means that (other that the root node) all internal nodes have at least ceil(3 / 2) = ceil(1.5) = 1 children (and hence at least 1 key). Of course, the maximum number of children that a node can have is 3 (so that 2 is the maximum number of keys). According to condition 4, each leaf node must contain at least 1 key.

Advantage of B-Tree over normal trees The number of values that a particular node of a binary search tree or an AVL tree can hold is only one. A B-tree can contain at the most two values per node. To improve the efficiency of operations performed on a tree , height of the tree has to be reduced. If a node contains more number of values then at a time more values can be accessed from the secondary medium. The time required to access the data from a secondary storage medium is very high. Hence, if we access the data less number of times, less would be the time required to perform an operation.

2. a. A circular linked list is a linked list in which the last node points to the first node, instead of having a NULL pointer. Thus in a circular linked list, the first and the last nodes are linked together. This can be done for both single and doubly linked lists. To traverse a circular linked list you begin at any node and follow the list in either direction until you return to the original node.

Abstract Data Type (ADT): A set of values and the core operations on those values, independent of a particular (computer) representation.AdvantagesCircular linked lists are most useful for describing naturally circular structures, and have the advantage of regular structure and being able to traverse the list starting at any point. They also allow quick access to the first and last records through a single pointer.

Page 9: Data Structures and Algorithm Analysis-0108

b. Spanning TreeA spanning tree of a graph is an undirected tree consisting of only those edges that are necessary to connect all the vertices in the original graph. It has the property that for any pair of vertices there exists only one path between them, and the insertion of any edge to a spanning tree form a unique cycle. Definition: Let G = ( N, E ) be an undirected connected graph, where N is set of nodes, E is set of edges. A sub graph T = ( N, E`) of graph G is a Spanning Tree iff T is a tree. Also E` is sub set of ESpanning Tree: A spanning tree is a sub graph of main graph G provided the following properties are satisfied. It should be connected. There should not be any circuits or cycles in it. Number of edges should be one les than the number of vertices. Number of vertices in a spanning tree is equal to number of vertices in the graph G.

The spanning tree resulting from a call to depth first tree is known as depth first spanning tree. Similarly, a spanning tree resulting from a call to breadth first tree is known as a breadth first spanning tree.Applications of spanning trees Analysis of electrical circuits. Shortest route problems.

3. a. ALGORITHM mColoring ( k )// This program was formed using the recursive backtracking schema. The graph is represented //by its Boolean adjacency matrix G[1.. n, 1..n ]. All assignments of // 1,2,...,m to the vertices of // the graph such that adjacent vertices are assigned distinct integers are printed. k is the index // of the next vertex to color.{

repeat{ // Generate all legal assignments for x [ k ].NextValue ( k ); // Assign to x [ k ] a legal color.if ( x [ k ] = 0 ) THEN break; // No new color possibleif ( k = n ) THEN // At most m colors have been used to color the n vertices.Write( x [ 1 .. n ] );elsemColoring(k+1);} until( false );}ALGORITHM NextValue ( k )// x[1,..., k-1 ] have been assigned integer values in the range [ 1 ,m ] such that // adjacent vertices have distinct integers. A value for x [ k ] is determined in the // range [0,m]. x [ k ] is assigned the next highest numbered color while maintaining// distinctness from the adjacent vertices of vertex k. If no such color exists, then // x [ k ] = 0{

Page 10: Data Structures and Algorithm Analysis-0108

repeat{

x [ k ] ß ( x [ k ] + 1 ) mod ( m + 1 ); // Next highest colorif ( x [ k ] = 0 ) THEN return; // All colors have been used.for j ß 1 to n do

{ // Check if this color is distinct from adjacent colors.if ( G [ k, j ] ≠ 0 ) and ( x [ k ] = x [ j ] ) ) THEN // If (k, j) is an edge and if adj. vertices have the same colorbreak;}if ( j = n+1 ) THEN return; // New color found} until ( false); // Otherwise try to find another color.}

b. HAMILTONIAN GRAPHLet G = ( V, E ) be a connected graph with ‘n’ vertices. A Hamiltonian is a path along ‘n’ edges of G that visit every vertex once and returns to its starting position. Suppose if the cycle begins with some vertex v1ε G and the vertices of G are visited in the order v1, v2, vn+1, then the edges( vi, vi+1) are in E, and v1 and vn+1 are equal. Algorithm Hamiltonian(k){

repeat{

NextVal(k); //Assigns a legal next value to x[k]if( x[k] = 0) then return;if( k = n) then write ( x[1..n]);elseHamiltonian(k+1);}until( false );}Algorithm NextVal(k)

// x[i:k-1] is a path of k-1 distinct vetices// if x[k] = 0 then no vertex yet has been assigned to x[k]// after execution x[k] is assigned to the next highest numbered vertex which does // not already appear inn x[1 : k-1 ]and is connected by an edge to x[k-1]// if k = n then x[k] is connected to x[1]{

repeat{x[k] := ( x[k] + 1 ) mod (n+1); // Next vertexif( x[k] = 0) then return ;if( G[x[k-1], x[k] ≠ 0 ) then // is there an edge{

for j:= 1 to k-1 do if( x[j] = x[k] ) then break;if( j = k ) then // if true, hen the vertex is distinctif( (k<n) or ( ( k= n) and g[x[n], x[1] ≠ 0) ) then return;

}}until( flase );

}

4. a. N – Queen’sALGORITHM Nqueens ( k, N)// Using backtracking, this procedure prints all possible// placements of N queens on an N X N chessboard so// that they are non attacking.

Page 11: Data Structures and Algorithm Analysis-0108

// N is number of queens, k is the value of queen considered{

for i ß 1 to N do{

if ( Place ( k, i ) ) then // if kth can be placed at ith column{

x[ k ] ß i; // store the value of ith columnif ( k = n ) then // if all the queens have been placedwrite ( x [ 1 .. N ] ); // Print the 8-tuple

else Nqueens ( k+1, N ); // Recursive call, for next row

}}}ALGORITHM Place( k, i )// Returns true if a queen can be placed in kth row and ith column// Otherwise it returns false. x[ ] is a global array whose first ( k-1 ) // values have been set.// abs( r ) returns the absolute value of r.{for j ß 1 to k – 1 do{// Two in the same column or in the same diagonal

if ( ( x [ j ] = i ) OR ( ABS ( x [ j ] – i ) = ABS ( j – k ) ) ) thenreturn ( false );

}return ( true );}

b. Knapsack using BacktrackingALGORITHM Bound( cp, cw, k)5// cp is the current profit total, cw is the current weight total; k // is the index of the last removed item; and m is the knapsack size {

b = cp; c = cw;for i ß k+1 to n DO{c ß c + w[i];if (c < m) THEN

b ß b p[i];else return( b + (1-(c-m)/w[i]) * p[i] );

}return(b);

}ALGORITHM BKnap( k, cp, cw)// m is the size of the knapsack; n is the number of weights and // profits. w[] and p are the weights and profits. p[i]/w[i] >= // p[i+1]/w[i+1]. fw is the final weight of knapsack; fp is the // final maximum profit. x[k]==0 if w[k] is not in the knapsack else x[k]==1.{

// Generate left child.if ( cw + w[k] <= m ) THEN{

y[k]ß 1; if (k<n) THEN BKnap( k+1, cp + p[k], cw + w[k]);

Page 12: Data Structures and Algorithm Analysis-0108

if ( (cp+p[k] > fp)&&(k==n)) THEN{

fp ß cp + p[k]; fw ß cw + w[k];for j ß 1 to k do x[j] = y[j];

}}

// Generate right child.if ( Bound(cp, cw, k) >= fp ) THEN {

y[ k ]=0; if ( k<n ) THEN BKnap( k+1, cp, cw );

if ( (cp > fp) AND (k = n) ) THEN {

fp = cp; fw = cw;for j ß 1 to k DO x[j] = y[j];

}}

}

5. a. P –Problem ( P CLASS )A problem is assigned to the P (polynomial time) class if the number of steps is bounded by polynomial. Let P be a problem and A be an algorithm, then P is called a ‘P class’ algorithm if and only if there exists two positive real numbers c, d such that the complexity of the algorithm can be expressed as O( d . nc ), where ‘n’ is the size of instanceA polynomial-time algorithm is an algorithm whose execution time is either given by a polynomial on the size of the input, or can be bounded by such a polynomial. Problems that can be solved by a polynomial-time algorithm are called tractable problems. For example, most algorithms on arrays can use the array size, n, as the input size. To find the largest element in an array requires a single pass through the array, so the algorithm for doing this is O(n), or linear time. Sorting algorithms usually require either O(n log n) or O(n2) time. Bubble sort takes linear time in the best case, but O(n2) time in the average and worst cases. Heap sort takes O(n log n) time in all cases. Quick sort takes O(n log n) time on average, but O(n2) time in the worst case. Regarding O(n log n) time, note that The base of the logarithms is irrelevant, since the difference is a constant factor, which we ignore; and Although n log n is not, strictly speaking, a polynomial, the size of n log n is bounded by n2, which is a polynomial. NP-Hard ProblemA problem is NP-hard if an algorithm for solving it can be translated into one for solving any otherNP-problem (nondeterministic polynomial time) problem. NP-hard therefore means "at least as hard as any NP-problem," although it might, in fact, be harder. NP-Complete ProblemA problem which is both NP (verifiable in nondeterministic polynomial time) and NP-hard (any other NP-problem can be translated into this problem). Examples of NP-hard problems include the Hamiltonian cycle and traveling salesman problems.

b. Dynamic Programming A dynamic programming algorithm remembers past results and uses them to find new results Dynamic programming is generally used for optimization problems

1. Multiple solutions exist, need to find the “best” one2. Requires “optimal substructure” and “overlapping subproblems”

– Optimal substructure: Optimal solution contains optimal solutions to subproblems– Overlapping subproblems: Solutions to subproblems can be stored and reused in a bottom-up fashion– This differs from Divide and Conquer, where subproblems generally need not overlap

Section C: Applied Theory

6. /* RECURSIVE PROGRAM TO CREATE, DISPLAY, AND COUNT THE NODES OF A LINKED LIST */# include <stdio.h>

Page 13: Data Structures and Algorithm Analysis-0108

# include <malloc.h># include <conio.h> /* CAN'TBE USED FOR gcc COMPILER */ypedef struct info{

int data;struct info *next;

}node;void main(){

void create( node * );void disp( node * );int count( node * );node *first = (node *) malloc( sizeof( node ) );create( first );printf( "\n\n\t The Elements Of List Are \n" );disp( first );getch();printf("\n The number of nodes are %d \n", count( first ) );getch();

}/* TO DISPLAY THE NODES OF LINKED LIST */void disp( node *first ){if( first->next != NULL){

printf("%4d", first->data);disp( first->next );

}* TO COUNT THE NODES OF THE LINKED LIST */nt count( node *first)

if( first->next == NULL)return( 0 );

elsereturn( 1+ count( first->next ));

}/* TO CREATE A LINKED LIST BY ADDNIG THE NODES AT THE END */void create( node *first){

int x;printf("\n Press ^Z to stop entering \n");

if( ( scanf("%d", &x) ) != EOF ){

first->data = x;first->next = (node *) malloc( sizeof ( node ) );create( first->next );

}else

first->next = NULL;}

7. # include <stdio.h># include <conio.h># define size 15void main()

Page 14: Data Structures and Algorithm Analysis-0108

{int a[size], i;void twmergesort( int *, int );clrscr();for( i=0; i<size; scanf( "%d", &a[i++]) )printf( "\n Please enter %d element : ", i+1);printf( "\n\tThe elements you entered are \n" );for( i=0; i<size; printf( "%5d", a[i++]) );twmergesort( a, size );printf( "\n\tThe elements after sorting are \n" );for( i=0; i<size; printf( "%5d", a[i++]) );getch();

}void twmergesort( int a[], int n ){

int l;void mpass( int *, int, int );l = 1;while( l < n ){

mpass( a, l, n );l = 2 * l;

}}void mpass( int a[], int l, int n ){

int i;void merge( int [], int, int, int );i = 0;while( i+2*l-1 < n ){

merge( a, i, i+l-1, i+2*l-1);i = i+2*l;

}if( i+l-1 < n-1 )

merge( a, i, i+l-1, n-1 );}void merge( int a[], int low, int mid, int high ){

int i, k, j, l;int b[size];i = k = low;j = mid + 1;while( ( i <= mid ) && ( j <= high ) )

if( a[i] <= a[j] )b[k++] = a[i++];

elseb[k++] = a[j++];

if( i > mid )for( l = j; l <= high; l++ )

b[k++] = a[l];else

for( l = i; l <= mid; l++ )

Page 15: Data Structures and Algorithm Analysis-0108

b[k++] = a[l];for( i = low; i <= high; i++ )

a[i] = b[i];}