2marks q&a - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. define polyphase...

37
DATA STRUCTURES / UNIT III 1 2MARKS Q&A 1. What does sorting mean? Ordering the data in an increasing or decreasing fashion, according to some linear relationship among the data items is called sorting. 2. What are the two main classification of sorting based on the source of data? According to the source of data sorting is classified into, a. External sorting b. Internal sorting 3. What does external sorting mean? External sorting is a process of sorting in which large blocks of data stored in storage device are moved to the main memory and then sorted. 4. What does internal sorting mean? Internal sorting is a process of sorting the data in the main memory 5. What are the various factors to be considered in deciding a sorting algorithm? Factors to be considered in deciding a sorting algorithm are, a. Programming time b. Executing time for program c. Memory or auxiliary space needed for the programs environment. 6. How does the bubble sort get its name? The bubble sort derives its name from the fact that the smallest data item bubbles up to the top of the sorted array. 7. What is the main idea behind the selection sort? The main idea behind the selection sort is to find the smallest entry among in a(j),a(j+1),……..a(n) and than interchange it with a(j). this process is then repeated for each value of j. 8. What is the basis of shell sort? Instead of sorting the entire array at once, it first divides the array into smaller segments, which are then separately sorted using the insertion sort. 9. What is the purpose of quick sort? The purpose of quick sort is to move a data item in the correct direction, just enough for it to reach its final place in the array.

Upload: truongdieu

Post on 03-Feb-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

1

22MMAARRKKSS QQ&&AA

1. What does sorting mean?

Ordering the data in an increasing or decreasing fashion, according to some linear relationship among the data items is called sorting.

2. What are the two main classification of sorting based on the source of data? According to the source of data sorting is classified into,

a. External sorting b. Internal sorting

3. What does external sorting mean? External sorting is a process of sorting in which large blocks of data stored in

storage device are moved to the main memory and then sorted.

4. What does internal sorting mean? Internal sorting is a process of sorting the data in the main memory

5. What are the various factors to be considered in deciding a sorting

algorithm? Factors to be considered in deciding a sorting algorithm are,

a. Programming time b. Executing time for program c. Memory or auxiliary space needed for the programs environment.

6. How does the bubble sort get its name? The bubble sort derives its name from the fact that the smallest data item bubbles

up to the top of the sorted array.

7. What is the main idea behind the selection sort? The main idea behind the selection sort is to find the smallest entry among in

a(j),a(j+1),……..a(n) and than interchange it with a(j). this process is then repeated for each value of j.

8. What is the basis of shell sort?

Instead of sorting the entire array at once, it first divides the array into smaller segments, which are then separately sorted using the insertion sort.

9. What is the purpose of quick sort?

The purpose of quick sort is to move a data item in the correct direction, just enough for it to reach its final place in the array.

Page 2: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

2

10. What is the advantage of quick sort?

Quick sort reduces unnecessary swaps and to moves an item to a greater distance in one move.

11. Is the heap sort always better than the quick sort? No, the heap sort does not perform better than the quick sort. Only when array is

nearly sorted to begin with the heap sort algorithm gains an advantage. In such a case, the quick deteriorates to its worst performance of O (n2).

12. Name some of the external sorting methods. Some of the external sorting methods are,

a. Polyphase sorting b. Oscillation sorting c. Merge sorting

13. When is a sorting method said to be stable? A sorting method is said to be stable if two data items of matching value are

guarantee to be not rearranged with respect to each other as the algorithm progresses.

14. What is the worst-case complexity of insertion sort? Worst-case complexity of insertion sort is,

W (n) = n(n-1)/2 = O(n2)

15. What is the average behavior of insertion sort? Average behavior of insertion sort is,

A(n) = n2/4 =O(n2) 16. What is the worst-case behavior of quick sort?

Worst-case behavior of quick sort is, W (n) = n(n-1)/2 = O(n2)

17. How does the bubble sort differ from the selection sort? In selection sort, we find the smallest record and then perform an interchange.

Whereas in bubble sort, two records are interchanged immediately upon discovering that they are out of order.

18. Define max heap.

A heap in which the parent has a larger key than the child’s is called a max Heap 19. Define min heap.

A heap in which the parent has a smaller key than the child’s is called a minHeap. 20. How many passes are required for k-way merging?

Page 3: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

3

The number of passes required using k-way merging is [logk(N/M)] because get k times as large in each pass.

21. Name some simple algorithms used in external sorting. Multiway merge, Polyphase merge ,Replacement selection.

22. When can we use insertion sort? Insertion sort is useful only for small files or very nearly sorted files.

23. How was merging performed in the early days? In the early days of data processing, merging was performed on cards with the aid

of a machine called a collector.

24. What is general algorithm used for a simple merge? The general algorithm used for a simple merge is given by,

a. Merge two ordered sub tables into a temporary vector. b. Copy the temporary vector into k where, k is the array of values.

25. Define two way merge ?

It is a basic external sorting in which there are two inputs and two outputs tapes.

26. Define multi way merge ? If we have extra tapes then we can expect to reduce the number of passes required

to sort our input. We do this by extending two way merge to a k-way merge.

27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be

prohibitive for some applications . It is possible to get by with only k+1 tapes. 28. What is replacement selection ?

We read as many records as possible and sort them . writing the result to some tapes. This seems like the best approach possible until one realizes that as soon as the first record is written to a output tape the memory it used becomes available for another record . if the next record on the input tape is larger than the record we have just output then it can be included in the item . using this we can give algorithm. This is called replacement selection.

29. What is Internal Key or Embedded key

The key is contained within the record at a specific offset from the start of the record, such a key is called an Internal key or an Embedded key.

30. What are External Keys?

There is a separate table of keys that includes pointers to the records. Such keys are called External Keys.

31. What are the types of searches?

Page 4: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

4

Internal Searches, External Searches are the types of Searches.

32. Define Internal Path length. The sum of the levels of all the nodes in the tree is called Internal path length.

33. Define External Path length. Replacing each null left or right pointer with a pointer to a separate, new leaf node

is called External node. The sum of the levels of all the external nodes in the tree is called External Path length.

34. Define Hash function.

Hash function takes an identifier and computes the address of that identifier in the hash table using some function. Hash function is used to perform insertions, deletions and finds in constant average time.

35. Define Hashing.

The address or location of an identifier x is obtained by computing some arithmetic function ‘f’ of ‘x’ i.e., f (x) gives the address of x in the table.

36. Define Multiway Search Trees.

A Multiway Search Tree of order n is a general tree in which each node has n or fewer subtrees and contains one fewer key than it has subrrees. (i.e) if a node has four subtrees, it contains three keys.

37. What is B+ Tree?

The B+ Tree retains the rapid random access property of B-trees, while also allowing rapid sequential access. All keys are maintained in leaves, and keys are replicated in nonleaf nodes to define paths for locating individual records.

38. Define Hash table.

The memory available to maintain a table of identifier is said to be a hash table, which is assumed to be sequential.

Hash table (ht) is partitioned into ‘b’ buckets ht [0]………ht [b-1] Example: Hash table 1-26 buckets with 2 slots Slots slot1 & slot2 Slot 1 Slot 2 0-indicates empty 1 2 3 4 . . . .

Page 5: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

5

39. List out the different types of Hashing functions. The different types of Hashing functions are, The division’s method. The mid square method The folding method. Digit analysis. The length dependent method. Algebraic coding. Multiplication hashing.

40. What are problems in hashing?

The problems in hashing are, Collision. Overflow.

41. Define collision in hashing.

When two different keys or identifiers compute into the same location or address in the hash table through any of the hashing functions, then it is termed Collision.

42. Define Double Hashing.

Double Hashing is a collision-resolution technique used in open addressing category. In double hashing, we apply a second hash function to x and probe at a distance of hash2 (x), 2hash2 (x)…………, and so on.

43. What are applications of hashing?

The applications of hashing are, Compliers use hash table to keep track of declared variables on source code. Hash table is useful for any graph theory problem, where the nodes have real

names instead of numbers. Hash tables are used in programs that play games. Online spelling checkers use hashing.

Page 6: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

6

1166 MMAARRKKSS QQ&&AA

Quick sort

The purpose of quick sort is to move a data item in the correct direction, just enough for it to reach its final place in the array. This is the efficient sorting technique, which is based on divide and conquers strategy. Its average running time is O(n log n). It is very fast, mainly due to a very tight and highly optimized inner loop. It has O(n2) worst-case performance, but this can be made exponentially unlikely with a little effort. The basic algorithm to sort an array S consists of the following four easy steps: 1. If the number of elements in S is 0 or 1, then return. 2. Pick any element v in S. This is called the pivot. 3. Partition S - {v} (the remaining elements in S) into two disjoint groups:S1 and S2. 4. Return { quicksort(S1) followed by v followed by quicksort(S2)}. Divide and Conquer: repeatedly cut problem into ``equal'' halves.

``Partition'' the list into two lists:

Invariant: numbers in list 1 <= list 2.

Partition by selecting a ``pivot'' element value. If the pivot is about the ``median'' then the lists will be same size. Scan inwards from left using i. Stop if a[i] >= pivot. Scan inwards from right using j. Stop if a[j] <= pivot. If i and j have not crossed each other, swap and continue scan. The scan is O(n). Repeat the process on the sublists. Get empty lists in O(log n). Quicksort: O( )

Example Pick the pivot to be the first element in the list: 6 Pick pivot:

1.Discuss the time require for quick sort with an algorithm Explain the quick sort algorithm with an example. Explain partition exchange sort algorithm in detail with an example. Write down the quick sort algorithm & give its worst case, best case, & average case analysis (May/Jun 05)

6 1 4 9 0 3 5 2 7 8

Page 7: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

7

Hide pivot:

Scan:

Swap:

Scan:

Swap:

Scan:

Swap pivot:

Repeat:

8 7 9

Median-of-3 Partitioning

New approach: compare the first, center, last element. Pick median. Also: sort the 3 elements.

8 1 4 9 0 3 5 2 7 6

8 1 4 9 0 3 5 2 7 6

i j

2 1 4 9 0 3 5 8 7 6

i j

2 1 4 9 0 3 5 8 7 6

i j

2 1 4 5 0 3 9 8 7 6

i j

2 1 4 5 0 3 9 8 7 6

j i

2 1 4 5 0 3 6 8 7 9

j i

2 1 4 5 0 3

Page 8: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

8

input_type median3( input_type a[], int left, int right ) { int center; center = (left + right) / 2; if (a[left] > a[center]) swap( &a[left], &a[center] ); if (a[left] > a[right]) swap( &a[left], &a[right] ); if (a[center] > a[right]) swap( &a[center], &a[right] ); /* invariant: a[left] <= a[center] <= a[right] */ swap( &a[center], &a[right-1] ); /* why? */ return( a[right-1] ); }

Quicksort:

% gcc -o quick quick.c > tcc quick.c Data File with Test Case: quick.dat: 10 6 1 4 9 0 3 5 2 7 8 % quick quick.dat 10 55 40 [ 0 1 2 3 4 5 6 7 8 9 ] 0 0

Quicksort: quick.dat % quick quick.dat step n move comp 1 2 3 4 5 6 7 8 9 10 i j 10 0 0 [ 6 1 4 9 0 3 5 2 7 8 ] 0 0 10 6 3 [ 0 1 4 9 7 3 5 2 6 8 ] 1 10 10 9 7 [ 0 1 4 2 7 3 5 9 6 8 ] 1 10 10 12 9 [ 0 1 4 2 5 3 7 9 6 8 ] 1 10 10 15 12 [ 0 1 4 2 5 3 6 9 7 8 ] 1 10 10 21 15 [ 0 1 5 2 3 4 6 9 7 8 ] 1 6 10 24 18 [ 0 1 2 5 3 4 6 9 7 8 ] 1 6 10 27 20 [ 0 1 2 3 5 4 6 9 7 8 ] 1 6 10 30 23 [ 0 1 2 3 5 4 6 9 7 8 ] 1 3 10 33 25 [ 0 1 2 3 5 4 6 9 7 8 ] 1 3 10 42 28 [ 0 1 2 3 5 4 6 7 8 9 ] 8 10 10 45 30 [ 0 1 2 3 5 4 6 7 8 9 ] 8 10 10 46 31 [ 0 1 2 3 5 4 6 7 8 9 ] 2 2 10 47 32 [ 0 1 2 3 5 4 6 7 8 9 ] 3 3 10 48 33 [ 0 1 2 3 5 4 6 7 8 9 ] 4 4 10 49 34 [ 0 1 2 3 5 4 6 7 8 9 ] 5 5 10 50 35 [ 0 1 2 3 5 5 6 7 8 9 ] 6 6 10 51 36 [ 0 1 2 3 4 5 6 7 8 9 ] 6 5 10 52 37 [ 0 1 2 3 4 5 6 7 8 9 ] 7 7 10 53 38 [ 0 1 2 3 4 5 6 7 8 9 ] 8 8 10 54 39 [ 0 1 2 3 4 5 6 7 8 9 ] 9 9 10 55 40 [ 0 1 2 3 4 5 6 7 8 9 ] 10 10 void q_sort( input_type a[], int left, int right ) { int i, j; input_type pivot; if (left + CUTOFF <= right) { /* CUTOFF should be 20 but example is 2 */

Page 9: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

9

pivot = median3( a, left, right ); i = left; j = right-1; /* why? */ for (;;) { while (a[++i] < pivot); while (a[--j] > pivot); if (i < j) swap( &a[i], &a[j] ); else break; } swap( &a[i], &a[right-1] ); /* why? */ q_sort( a, left, i-1 ); q_sort( a, i+1, right); } } void quick_sort( input_type a[], int n ) { q_sort( a, 1, n); insertion_sort( a, n ); /* n<=20: insertion is better, use CUTOFF */ }

Worst-Case:

Let i= length of the first sublist. T(n)= running time on a list of n elements. T(1) = 1 T(n)= running time on two sublists + linear time on parititioning. T(n)=T(i) + T(n-i-1) +cn Worst-Case: pivot is smallest element and i=0. T(n)=T(0) + T(n-1) +cn which is approximately T(n-1) + cn Telescope: T(n-1) = T(n-2) + c(n-1) T(n-2) = T(n-3) + c(n-2)

T(2) = T(1) + c(2)

Best-Case:

Best-Case: pivot is in middle and i=n/2. T(n) = T(n/2) + T(n/2) + cn=2T(n/2)+cn Divide by n:

Telescope:

Page 10: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

10

...

There are log n of the above equations.

Average-Case: Average-Case: each i=0..n-1 is equally likely with probability 1/n.

Similarly:

Subtract: nT(n)-(n-1)T(n-1) = 2 T(n-1) + 2cn-c Rearrange and drop -c: nT(n) = (n+1)T(n-1) + 2cn Divide by n(n+1):

Page 11: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

11

Quicksort:

Telescope:

...

Summing:

T(n) = O( )

Write down the merge sort algorithm & give its worst case, best case, average case analysis (nov/dec 05) Merge sort In this method, we divide the main list into two sub lists, then go on dividing those sub lists till we get sufficient length of that sub lists. We compare & sort the elements of

2.Write a procedures to sort the given no using merge sort Explain the merge sort algorithm in detail perform time analysis of merge sort algorithm

Page 12: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

12

each list. Merge the two-sub list & the merged list This process will be repeated until we get only one sorted it.

The fundamental operation in this algorithm is merging two sorted lists. Because the lists are sorted, this can be done in one pass through the input, if the output is put in a third list. The basic merging algorithm takes two input arrays a and b, an output array c, and three counters, aptr, bptr, and cptr, which are initially set to the beginning of their respective arrays. The smaller of a[aptr] and b[bptr] is copied to the next entry in c, and the appropriate counters are advanced. When either input list is exhausted, the remainder of the other list is copied to c. An example of how the merge routine works is provided for the following input.

If the array a contains 1, 13, 24, 26, and b contains 2, 15, 27, 38, then the

algorithm proceeds as follows: First, a comparison is done between 1 and 2. 1 is added to c, and then 13 and 2 are compared.

2 is added to c, and then 13 and 15 are compared.

13 is added to c, and then 24 and 15 are compared. This proceeds until 26 and 27

are compared.

26 is added to c, and the a array is exhausted.

The remainder of the b array is then copied to c.

Page 13: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

13

The time to merge two sorted lists is clearly linear, because at most n – 1 comparisons are made, where n is the total number of elements. If n = 1, there is only one element to sort, and the answer is at hand. Otherwise, recursively mergesort the first half and the second half. This gives two sorted halves, which can then be merged together using the merging algorithm described above. For instance, to sort the eight-element array 24, 13, 26, 1, 2, 27, 38, 15, we recursively sort the first four and last four elements, obtaining 1, 13, 24, 26, 2, 15, 27, 38. Then we merge the two halves as above, obtaining the final list 1, 2, 13, 15, 24, 26, 27, 38. This algorithm is a classic divide-and-conquer strategy. The problem is divided into smaller problems and solved recursively. The conquering phase consists of patching together the answers. Divide-and-conquer is a very powerful use of recursion that we will see many times.

An implementation of mergesort is void mergesort( input_type a[], unsigned int n ) { input_type *tmp_array; tmp_array = (input_type *) malloc ( (n+1) * sizeof (input_type) ); if( tmp_array != NULL ) { m_sort( a, tmp_array, 1, n ); free( tmp_array ); } else fatal_error("No space for tmp array!!!"); } void m_sort( input_type a[], input_type tmp_array[ ], int left, int right ) { int center; if( left < right ) { center = (left + right) / 2; m_sort( a, tmp_array, left, center ); m_sort( a, tmp_array, center+1, right ); merge( a, tmp_array, left, center+1, right ); } }

Page 14: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

14

/* 1_pos = start of left half, r_pos = start of right half */ void merge( input_type a[ ], input_type tmp_array[ ], int l_pos, int r_pos, int right_end ) { int i, left_end, num_elements, tmp_pos; left_end = r_pos - 1; tmp_pos = l_pos; num_elements = right_end - l_pos + 1; /* main loop */ while( ( 1_pos <= left_end ) && ( r_pos <= right_end ) ) if( a[1_pos] <= a[r_pos] ) tmp_array[tmp_pos++] = a[l_pos++]; else tmp_array[tmp_pos++] = a[r_pos++]; while( l_pos <= left_end ) /* copy rest of first half */ tmp_array[tmp_pos++] = a[l_pos++]; while( r_pos <= right_end ) /* copy rest of second half */ tmp_array[tmp_pos++] = a[r_pos++]; /* copy tmp_array back */ for(i=1; i <= num_elements; i++, right_end-- ) a[right_end] = tmp_array[right_end]; }

The procedure called mergesort is just a driver for the recursive routine m_sort. The merge routine is subtle. If a temporary array is declared locally for each recursive call of merge, then there could be log n temporary arrays active at any point. This could be fatal on a machine with small memory. On the other hand, if the merge routine dynamically allocates and frees the minimum amount of temporary memory, considerable time will be used by malloc. A close examination shows that since merge is the last line of m_sort, there only needs to be one temporary array active at any point. Further, we can use any part of the temporary array; we will use the same portion as the input array a Analysis of Mergesort

Assume that n is a power of 2, so that we always split into even halves. For n = 1, the time to mergesort is constant, which we will denote by 1. Otherwise, the time to mergesort n numbers is equal to the time to do two recursive mergesorts of size n/2, plus the time to merge, which is linear.

T(1) = 1 T(n) = 2T(n/2) + n

This equation is valid for any n that is a power of 2, so we may also write

Page 15: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

15

and

T(n) = n log n + n = O(n log n) T(n) = nT(1) + n log n = n log n + n

What does external sorting mean? Name any two external sorting method & explain one with an example EXTERNAL SORTING

The basic external sorting algorithm uses the merge routine from the merge sort. Let us consider an example of external sorting. The input stored on a tape are

these are the nos to be sorted using external sorting algorithm. Assume there are 4 tapes Ta1, Ta2, Tb1,Tb2 which are two input & output tapes.

The tapes a & b can act as either input or output tapes depending on the point in the algorithm. Now consider the data (i/p) is initially present at tape Ta1 (ie) . Ta1 is now holding all the 13 elements to be sorted.

Suppose further that the internal m/m can hold & sort M records at a time. The

first step of algorithm is to read M records at a time from i/p tape, sort the records internally & then write the sorted records alternately to Tb1 & tb2 For ex: If M= 3, then take 3 records at a time from the i/p tape (Ta1), sort them internally & store elements in Tb1. 81 94 11 M = 2 11 81 94 sorted order next take the next three records from Ta1 96 12 35 M = 2 12 35 96 sorted order

3.what is external sorting? give the polyphase merging strategy with three tapes T1, T2, T3 (may/jun 05) use only two tapes Ta1 &Ta2 &perform external sorting using an example

Page 16: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

16

Next three records are 17 99 82 next three records. 17 28 99 Store it in Tb1. Similarly, 58 41 75 41 58 75 Store it in Tb1 The remaining element left out is 15.

15 - store in Tb1.

Now the merge sort algorithm to merge Tb1 & Tb2 and store it alternatively in Ta1 & Ta2. Take Tb1 and Tb2 in first run Merge these records 11 12 35 81 94 96 Store it in Ta1 Similarly take the elements of Tb1 & Tb2 in the second run.They are 17 28 99 Tb1 41 58 75 Tb2 Apply merge sort. 71 28 41 58 75 99 store it in Ta2. Take the elements of Tb1 & Tb2 in third run, element left out is 15. Store it in Ta1. Now take Ta1 & Ta2. Apply merge sort & store it in Tb1 & Tb2 alternatively 11 12 35 81 94 96 Ta1 17 28 41 58 75 99 Ta2. Apply merge sort & store it in Tb1. The sorted order is 11 12 17 28 35 51 58 75 81 94 96 99 Now perform the second run, & store in Tb2. In the second run the only element left 99 out is 15. it is stored in Tb2. Now take Tb1 & Tb2. Apply merge sort & store it in Ta1 This algorithm will require [log(N/M)] passes, plus the initial run- constructing pass. Multiway merge Multiway merge is used to reduce the no of passes required to sort the i/p. we do this y extending the basis of two way merge to a K-way merge. If there are K i/p tapes, the strategy of multiway merge works the same way as two way merge but with a slight complication of finding the smallest element. The example is given below with 6 tapes Ta1,Ta2,Ta3,Tb1,Tb2,Tb3. In the next stage we need two more passes to complete the sort. Finally the sorted list is taken from the output tape Tb1. Bubble sorting In bubble sort each element is compared with its adjacent element. If the first element is larger than the second one then the position of the elements are interchanged otherwise it is not changed. Then next element is compared with its adjacent element and the same process is repeated for all the elements in the array. During the next pass the same process is the largest leaving the largest element. During the pass the second largest element occupies the second last position. During the next pass the same process is repeated leaving the largest element. During this pass the largest element occupies the n-1

Page 17: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

17

position. The same process is repeated until no more elements are left comparison. Finally the array sorted one. The various steps involved in sorting of an 5 elements is given below: 11 15 2 13 6 FIRAT PASS 11 no swapping 11 11 11 15 2 swapped 2 2 2 15 13 swapped 13 13 13 15 6 swapped 6 6 6 15 SECOND PASS 2 swapped 2 2 2 11 11 no swapped 11 11 13 13 6 swapped 6 6 6 13 13 no swapped15 15 15 15 THIRD PASS 2 no swapped 2 2 2 11 6 swapped 6 6 6 11 11 no swapped 11 13 13 13 13 n swapped 15 15 15 15 Algorithm Let a be a linear array of elements temp be a temporary variable for interchanging the position of elements.

1. Input n elements of an array a. 2. Initialize I= 0 3. Repeat through step 6 while (I<n) 4. set j= 0 5. repeat through step 6 while (j<n-I-1) 6. if (a[j]> a[j+1])

i) temp=a[j] ii) a[j]=a[j+1] iii) a[j+1]=temp

7. Display the sorted elements of array a. 8. Exit.

Selection sorting

Page 18: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

18

The selection sort technique is based upon the extension of the minimum maximum tecnique. By means of a nest of for loops a pass through the array is made to locate the minimum value. Once this found it is placed in the first position of the array. Another the remaining element is made to find the next smallest element, which is placed in the second position and so on. Once the next to last element is compared with the last element all the elements have been sorted into ascending order. More precisely: Let a be a linear array of n elements Pass 1.Find the location loc of the smallest in the list of n elements A[1],a[2],……, a[n] and then interchanged a[loc] and a[1] then Pass 2.Find the location loc of the smallest in the sub list of n-1 elements A[2],a[3],……, a[n] and then interchanged a[loc] and a[2] then A[1],a[2]is stored since a[1] <= a[2]

Pass 2.Find the location loc of the smallest in the sub list of n-2 elements A[3],a[4],……, a[n] and then interchanged a[loc] and a[3] then A[1],a[2],…..a[3]is stored since a[1] <= a[2] Pass n-1.Find the location loc of the smallest of elements a[n-1],a[n ]……, a[n] and then interchanged a[loc] and a[3] then A[1],a[2],…..a[3]is stored since a[n-1] <= a[n] For example suppose we have a list which contains the elements: 40,3050,20 and 10. If we want to sort technique the details of this technique are illustrated in fig Procedure min(a,k,n,loc) An array a is in memory. This procedure finds the location loc of the smallest element among a[k],a[k+1],…..a[n] during k pass. Min(a,k,n,loc)

1. set min:= a[k] and loc :=k[initializes pointer] 2. Repeat for j=k+1,k+2 …… n:

if min> a[j] then: set min:=a[j] and loc;=j [end of loop]

3. Return Algorithm This algorithm can now be easily started 1.Repeat steps 2 and 3 for k= 1,2,…… n-1 2. Call min(a,k,nn,loc)

3. [Interchange a[k] and a[loc] Set temp:= a[k], a[k] :=a[loc] : = temp [End of step 1 loop]

4.Exit

Page 19: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

19

Construct a heap for the array structure. Check whether the heap property is maintained. If it is violated rearrange the heap show as to maintain the heap property 4 18 7 36 72 54 11 24 45 3

Heap as a Priority Queue: A descending priority queue using a descending heap. a descending heap of size k. Because the priority queue is contained in array elements 0 to k-1 we add K as a parameter of the insertion & detection operations. Example for Heap sort: An Example of Heapsort: Given an array of 6 elements: 15, 19, 10, 7, 17, 16, sort it in ascending order using heap sort. Steps:

1. Consider the values of the elements as priorities and build the heap tree. 2. Start deleteMin operations, storing each deleted element at the end of the heap array.

After performing step 2, the order of the elements will be opposite to the order in the heap tree. Hence, if we want the elements to be sorted in ascending order, we need to build the heap tree in descending order - the greatest element will have the highest priority.

Note that we use only one array , treating its parts differently:

a. when building the heap tree, part of the array will be considered as the heap, and the rest part - the original array.

b. when sorting, part of the array will be the heap, and the rest part - the sorted array.

This will be indicated by colors: white for the original array, blue for the heap and red for the sorted array

Here is the array: 15, 19, 10, 7, 17, 6

A. Building the heap tree The array represented as a tree, complete but not ordered:

4.Explain heap sort algorithm in detail with an example Show how heap sort process the input 142,543,123,65,453,879,572,434,111,242,811,102 (Nov/Dec 05) Explain tree sort algorithm with an example. (Nov/Dec 03) Illustrate inserting an element into a heap with following no explain the stages of heap sort with an example (Nov/Dec 05)

Page 20: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

20

A. Start with the rightmost node at height 1 - the node at position 3 = Size/2. It has one greater child and has to be percolated down:

After processing array[3] the situation is:

Next comes array[2]. Its children are smaller, so no percolation is needed.

Page 21: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

21

The last node to be processed is array[1]. Its left child is the greater of the children. The item at array[1] has to be percolated down to the left, swapped with array[2].

As a result the situation is:

The children of array[2] are greater, and item 15 has to be moved down further, swapped with array[5].

Page 22: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

22

Now the tree is ordered, and the binary heap is built.

B. Sorting - performing deleteMax operations:

1. Delete the top element 19.

1.1. Store 19 in a temporary place. A hole is created at the top

1.2. Swap 19 with the last element of the heap.

As 10 will be adjusted in the heap, its cell will no longer be a part of the heap. Instead it becomes a cell from the sorted array

Page 23: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

23

1.3. Percolate down the hole

1.4. Percolate once more (10 is less that 15, so it cannot be inserted in the previous hole)

Now 10 can be inserted in the hole

Page 24: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

24

2. DeleteMax the top element 17

2.1. Store 17 in a temporary place. A hole is created at the top

2.2. Swap 17 with the last element of the heap.

As 10 will be adjusted in the heap, its cell will no longer be a part of the heap. Instead it becomes a cell from the sorted array

Page 25: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

25

2.3. The element 10 is less than the children of the hole, and we percolate the hole down:

2.4. Insert 10 in the hole

3. DeleteMax 16

3.1. Store 16 in a temporary place. A hole is created at the top

Page 26: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

26

3.2. Swap 16 with the last element of the heap.

As 7 will be adjusted in the heap, its cell will no longer be a part of the heap. Instead it becomes a cell from the sorted array

3.3. Percolate the hole down (7 cannot be inserted there - it is less than the children of the hole)

3.4. Insert 7 in the hole

4. DeleteMax the top element 15

4.1. Store 15 in a temporary location. A hole is created.

4.2. Swap 15 with the last element of the heap.

Page 27: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

27

As 10 will be adjusted in the heap, its cell will no longer be a part of the heap. Instead it becomes a position from the sorted array

4.3. Store 10 in the hole (10 is greater than the children of the hole)

5. DeleteMax the top element 10.

5.1. Remove 10 from the heap and store it into a temporary location.

5.2. Swap 10 with the last element of the heap.

As 7 will be adjusted in the heap, its cell will no longer be a part of the heap. Instead it becomes a cell from the sorted array

Page 28: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

28

5.3. Store 7 in the hole (as the only remaining element in the heap

7 is the last element from the heap, so now the array is sorted

5a). Describe open hashing and closed hashing techniques in detail. (OR) b) Write routines to find and to insert an element in a separate Chaining hash table. (May/Jun 2005)(OR) c) Explain the general idea of hashing with an example. (Apr/May 2004)(OR) d) Explain in detail about Hashing techniques.(Nov/Dec 2003) Hashing Function:

1. Hashing function is called as keys to address transformation. 2. A hashing function acts upon a given key to return the relative address

(position) in the list where we except to find the key. Example: inputs 3, 5, 8, 10

Page 29: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

29

With max.record size = 4 Hashing Function: Division Remainder Method Hash (Key value) = (key value mod array size) +1 by using this calculation we can find the address. 3, 5, 8, 10 – record size (or) Array Size =4 0 Hash (3) = ( 3 mod 4) + 1 =4 0 Hash (5) = ( 5 mod 4) + 1 =2 0 Hash (8) = ( 8 mod 4) + 1 =1 0 Hash (10) = ( 10 mod 4) + 1 = 3 The position 1 for storing 8 and remaining is also follows same method. If we wand to search any number we can easily calculate its position and we can compare the search element with the value at that position.

Example: S = 10 Hash (10) =(10 mod 4) + 1= 3 S =10, 3rd position, element = 10 10 =10 search success. Eg: The values 4, 5, 8, 10 with the array size =4 1 0 0 Hash (4) = (4 mod 4) + 1 =1 2 0 Hash (5) = (5 mod 4) + 1 =2 3 0 Hash (8) = (8 mod 4) + 1 =1 4 0 Hash (10) = (10 mod 4) + 1 = 3

8 5 10 3

Page 30: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

30

1 4 8 2 5 3 4 10 Here after key value 4, 8 are returning the same address 1 so we can’t store both of that values at the corresponding place. This problem is called collision. Collision:

If two or more key values returning the same address the problem is referred as collision. The key value that cause collisions are called synonyms.

Here 4, 8 are synonyms.

Collision can be avoided by two methods, Linear collision processing (or) linear probing (or) Open addressing Linked collision processing (or) separate Chaining.

Open Addressing (Linear Probing)

1. Linear collision processing: 1. The linear collision processing method is the simplest method to

implement. 2. When collision occurs, we proceed down the list. 3. In sequential order until we find a vacant position. The key causing the

collision is then placed at this first vacant position. 4. If we come to the physical end of our list in the attempt to place the

problem key, we memory wrap around to the top of the list and keep cooking for the vacant position.

E.g.: Hashing Function = (Key range mod array size) + 1 Input: 18, 31, 67, 36, 19, and 34 With the max record size = 7 Initial Hash table

Page 31: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

31

Hash (18)=18(mod7)+1=5 Hash (31)=(31 mod 7)+1=4

1st collision

Hash (67)=5 Hash (36)=(36 mod 7)+1=2 Hash (19)=6 1st collision 2nd collision Free space at 6 Free space at 7

3rd collision Hash(34)=7 3rd collision, no downward space move up to first free space

at 1.

Key Key value

1 0 2 0 3 0 4 0 5 0 6 0 7 0

Key Key value

1 0 2 0 3 0 4 0 5 18 6 0 7 0

Key Key value

1 0 2 0 3 0 4 31 5 18 6 0 7 0

Key Key value

1 0 2 0 3 0 4 31 5 18 6 67 7 0

Key Key value

1 0 2 36 3 0 4 31 5 18 6 67 7 0

Key Key value

1 0 2 36 3 0 4 31 5 18 6 67 7 19

Key Key value

1 34 2 36 3 0 4 31 5 18 6 67 7 19

Page 32: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

32

If we want to search any element in Hash table. We can apply the same

hashing function for getting address and can easily search the element. Algorithm

1. Read S. 2. Find address by calling hashing function with S as argument. 3. Compare S with the corresponding address element Matches – Search Success.

Else Collision we have find new address Repeat 3 4. Stop

1. FOUND = FALSE 2. READ (S) 3. I = HASH (S) Calling Hashing Function

J = I 4. WHILE (! FOUND) BEGIN IF (KEY [J] =S) FOUND = TRUE ELSE J = (J MOD ARRAY SIZE) + 1 END

5. END. E.g:

(i) S=18 Hash (18) = (18 mod 7)+1 = 5 5th position element =18 S=5th position element 18=18 TRUE FOUND

Key Key value

1 34 2 36 3 0 4 31 5 18 6 67 7 19

Key Key value

1 34 2 36 3 0 4 31 5 18 6 67 7 19

Page 33: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

33

2. S= 19 Hash (19) = (19 mod 7) + 1 =6 6th position element = 67 S = 6th position element 19 = 67 false J = (6 mod 7) + 1 =7 collision 7th position element = 19 S = 7th position element 19 = 19 True FOUND Disadvantages:

1. Clustering efficiency is low. 2. One collision will cause the next one

OPEN HASHING (SEPARATE CHAINING)

Linked collision processing (or) chaining

1. Linked collision processing completely eliminates the possibility that one

collision could be getting another. 2. It requires the storage area directed horizontally into 2 regions.

1. Primary hash area. 2. Over flow area.

3.Each record requires a LINK field, in addition to the key and key value. 4. The global variable ARRRAY SIZE h applicable to prime hash area only. E.g.: Storage representation Key Key value Link 1 Prime hash area 2 n n+1 Over flow area

Page 34: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

34

1. Initially the hashing function translates keys into the prime hashing area. 2. If collision occurs, the key is inserted in to a linked list with its initial node in the

prime area and all following nodes in the overflow area. E.g.: The hashing function

Hash (key value) = (key value and array size) + 1 E.g.: Input 22, 31, 67, 36, 29, and 60 with array size 7.

Initial hash table Key Key value Link

Prime hash area

Overflow area

Hash (22) = 2, initially no collision Hash (31) = 4, initially no collision Link = null Link = null

1 0 N 2 0 N 3 0 N 4 0 N 5 0 N 6 0 N 7 0 N

8 9 10 11 12

Key Key value Link 1 0 N 2 22 N 3 0 N 4 0 N 5 0 N 6 0 N 7 0 N

8 9 10 11 12

Key Key value Link 1 0 N 2 22 N 3 0 N 4 31 N 5 0 N 6 0 N 7 0 N

8 9 10 11 12

Page 35: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

35

Hash (67) = 5, initially no collision Hash (36) = 2, 1st collision Link = null Link to overflow area

Hash (29)=2,2nd collision Hash (60)=5,3rd collision Already link to overflow area, Link to overflow area Link from overflow area to itself

Key Key value Link 1 0 N 2 22 8 3 0 N 4 31 N 5 67 N 6 0 N 7 0 N

8 36 N 9 10 11 12

Key Key value Link 1 0 N 2 22 N 3 0 N 4 31 N 5 67 N 6 0 N 7 0 N

8 9 10 11 12

Key Key value Link 1 0 N 2 22 8 3 0 N 4 31 N 5 67 10 6 0 N 7 0 N

8 36 9 9 29 N 10 60 N 11 12

Key Key value Link 1 0 N 2 22 8 3 0 N 4 31 N 5 67 N 6 0 N 7 0 N

8 36 9 9 29 N 10 11 12

Page 36: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

36

If we want to search any element in hash table Again we can apply same thing. (i.e.) Hashing Function for getting address.

Algorithm 1. Read S 2. Find address by calling hashing function with S as parameter. 3. Compare S with the corresponding position element match – Search

Success. 4. Else (collision)

Check the link field address. 5. Repeat 3. 6. Stop

1. READ (S). 2. I = HASH (S). 3. FOUND = FALSE

REPEAT IF (S = KEY [I]) THEN WRITE (FOUND) ELSE I = LINK (I) UNTIL [FOUND].

4. STOP.

Key Key value Link 1 0 N 2 22 8 3 0 N 4 31 N 5 67 10 6 0 N 7 0 N

8 36 9 9 29 N 10 60 N 11 12

Page 37: 2MARKS Q&A - chettinadtech.ac.inchettinadtech.ac.in/storage/three.pdf · 27. Define polyphase merge ? The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive

DATA STRUCTURES / UNIT III

37

E.g.:

S = 31 Hash (31) = (31 mod 7) + 1

= 4 4th position element = 31 S = 4th position element 31 = 31 FOUND S = 29

Hash (29) = (29 mod 7) + 1 =2 2nd position element = 22 S = 2nd position element

22 = 29 False collision check link address = 8 S = 8th position element. 29 = 36 False collision check link address = 9 S = 9th position element.

29 = 29 Linked collision method is better method compare to linear

method.

AASSSSIIGGNNMMEENNTT QQUUEESSTTIIOONNSS

1. Explain the sorting schemes, with several keys. Give the algorithm for

Heap sort and discuss how it works.

2. Differentiate between heap sort and Radix sort. Explain insertion sort

algorithm and trace the algorithm with an example.

Key Key value Link 1 0 N 2 22 8 3 0 N 4 31 N 5 67 10 6 0 N 7 0 N 8 9 9 10 N 11 N