CS301-Lec45 handout - CS301 Data Structures Lecture No. 45...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 45 ___________________________________________________________________ Data Structures Lecture No. 45 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 7 7.6, 7.7 Summary Divide and Conquer Mergesort mergeArrays Mergesort and Linked Lists Mergesort Analysis Quicksort Course Overview Divide and Conquer In the previous lecture, we had started discussing three new sorting algorithms; merge sort, quick sort and heap sort . All of these three algorithms take time proportional to nlog 2 n . Our elementary three sorting algorithms were taking n 2 time; therefore, these new algorithms with nlog 2 n time are faster. In search operation, we were trying to reduce the time from n to log 2 n . Let’s discuss these sorting algorithms; merge sort, quick sort and heap sort in detail. We had started our discussion from divide and conquer rule where we also saw an example. Instead of sorting a whole array, we will divide it in two parts, each part is sorted separately and then they are merged into a single array. Page 1 of 24
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 45 ___________________________________________________________________ 10 12 8 4 2 11 7 5 Split the list into two parts 4 8 10 12 2 5 7 11 Sort the two parts 4 8 10 12 2 5 7 11 Merge the two parts together Fig 45.1 Let’ see few analysis to confirm the usefulness of the divide and conquer technique. - To sort the halves approximate time is (n/2) 2 +(n/2) 2 - To merge the two halves approximate time is n - So, for n=100, divide and conquer takes approximately: = (100/2) 2 + (100/2) 2 + 100 = 2500 + 2500 + 100 = 5100 ( n = 10,000)  We know that elementary three sorting algorithms were taking approximately n 2 time. Suppose we are using insertion sort of those elementary algorithms. We divide the list into two halves then the time will be approximately (n/2) 2 +(n/2) 2 . The time required for merging operation is approximately n . This operation contains a simple loop that goes to n . Suppose that n is 100. Considering if we apply insertion sort algorithm on it then the time taken will be approximately (100) 2 = 10000. Now, if we apply divide and conquer technique on it. Then for first half approximate time will be (100/2) 2 . Similarly for second half it will be (100/2) 2 . The merging approximate time will be 100 . So the whole operation of sorting using this divide and conquer technique in insertion sort will take around (100/2) 2 + (100/2) 2 +100 = 5100 . Clearly the time spent (5100) after applying divide and conquer mechanism is significantly lesser than the previous time (10000). It is reduced approximately to half of the previous time. This example shows the usefulness of divide and conquer technique. By looking at the benefit after dividing the list into two halves, some further questions arise: - Why not divide the halves in half? Page 2 of 24
Background image of page 2
CS301 – Data Structures Lecture No. 45 ___________________________________________________________________ - The quarters in half? - And so on . . . - When should we stop? At n = 1 So we stop subdividing the list when we reach to the single element level. This divide and conquer strategy is not a thing, we have already prepared binary search tree on the same lines. One side of the tree contains the greater elements than the root and other part contains the smaller elements. Especially, when performing binary search in an array, we
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

CS301-Lec45 handout - CS301 Data Structures Lecture No. 45...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online