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.
 Fall '08
 Staff
 Data Structures, Quick Sort, void quicksort, teh original quicksort

Click to edit the document details