CS301-Lec45 handout

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

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

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

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

View Full Document
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
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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern