{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

notes96

# notes96 - Merge sort Complexity Each element is copied a...

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

Merge sort Complexity Each element is copied a number of times proportional to the height of the tree. height = log n Hence, n elements are copied a number of times proportional to n log n Merge sort complexity is O ( n log n) Quick Sort Popular sorting algorithm that also uses divide and conquer. void quicksort(S) Input: unsorted array S Output : sorted array S 1. Divide If S has only 1 element then return else{ - choose an element X in S that we will call pivot - Divide S into 3 sets L, E, G where the elements in L are less than X the elements in G are greater than X the elements in E are equal to X 2. Recur quicksort(L) quicksort(G) 3. Conquer put elements of L, E, G back into S Eg: quicksort(7, 6, 2, 10, 4, 5, 9, 8) pivot L E G qs(7, 6, 2, 4, 5) 8 qs(10, 9) L E G L E G qs(2, 4) 5 qs(7,

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

View Full Document
6) Empty 9 qs(10) L E G L E G empty empty qs(2) 4 Empty Empty 6 qs(7) 10 empty empty empty empty 2 7 2 4 5 6 7 8 9 10 Quicksort works well in the average case( O(nlogn)) but in worst case becomes O(n 2 ) When L & G are empty, then it becomes bubblesort Quicksort also does not need additional memory. It is faster than heap sort.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

notes96 - Merge sort Complexity Each element is copied a...

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

View Full Document
Ask a homework question - tutors are online