lecture12CS32Nachenburg

# lecture12CS32Nachenburg - 1 We sday Fe 24 dne b orting...

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

1 Wednesday, Feb 24 th Sorting Algorithms, part II: Quicksort Mergesort Introduction to Trees

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

View Full Document
2 Divide and Conquer Sorting Any time you see “ divide and conquer ,” you should think recursion ... EEK! The last two sorts we’ll learn (for now) are Quicksort and Mergesort . 1. Divide the elements to be sorted into two groups of roughly equal size. 2. Sort each of these smaller groups of elements (conquer). 3. Combine the two sorted groups into one large sorted list. These sorts generally work as follows:
3 The Quicksort Algorithm 1. Select an arbitrary element P from the array (typically the first element in the array). 1. If the array contains only 0 or 1 element, return . 1. Move all elements that are less than or equal to P to the left of the array and all elements greater than P right (this is called partitioning ). Divide 4. Recursively repeat this process on the left sub-array and then the right sub-array. Conquer

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

View Full Document
4 QuickSort USC Grad History Major Bio EE CS MBA Drop-out Select an arbitrary item P from the array. Move items smaller than or equal to P to the left and larger items right ; P goes in-between. Recursively repeat this process on the left items P Everything on this side is smaller than item P! larger And item P is exactly in the right spot in between! right items
5 QuickSort CS Major MBA Drop-out Select an arbitrary item P from the array. Move items smaller than or equal to P to the left and larger items right ; P goes in-between. Recursively repeat this process on the left items right items P P P Everything left of EE Major (our first P) is now sorted !

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

View Full Document
6 QuickSort USC Grad History Major Bio EE MBA P P 2 Select an arbitrary item P from the array. Move items smaller than or equal to P to the left and larger items right ; P goes in-between. Recursively repeat this process on the left items right items P P P P 3 Everything right of EE Major (our first P) is now sorted ! Finally, all items are sorted!
7 0 7 D&C Sorts: Quicksort And here’s an actual Quicksort C++ function: void QuickSort(int Array[],int First,int Last) { if (Last – First >= 1 ) { int PivotIndex; PivotIndex = Partition (Array,First,Last); QuickSort (Array,First,PivotIndex-1); // left QuickSort (Array,PivotIndex+1,Last); // right } } First specifies the starting element of the array to sort. Last specifies the last Only bother sorting arrays of at least two elements! DIVIDE Pick an element. Move <= items left Move > items right CONQUER Apply our QS algorithm to the left half of the array. the right half of the array. 3

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

View Full Document
8 int Partition(int a[], int low, int high) { int pi = low; int pivot = a[low]; do { while ( low <= high && a[low] <= pivot ) low++; while ( a[high] > pivot ) high--; if ( low < high ) swap(a[low], a[high]); } while ( low < high ); swap(a[pi], a[high]); pi = high; return(pi); } The QS Partition Function 30 1 77 13 99 52 40 4 12 35 47 56 The Partition function uses the first item as the pivot value and moves smaller items to the left and larger ones to the right. } – Select the first item as our pivot value high low Find first value > than the pivot. }- <= than the pivot. Swap the larger with the smaller Find next value 4 done
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/11/2010 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

### Page1 / 68

lecture12CS32Nachenburg - 1 We sday Fe 24 dne b orting...

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

View Full Document
Ask a homework question - tutors are online