notes61

# notes61 - Quick Sort Popular sorting algorithm that uses...

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

Quick Sort Popular sorting algorithm that uses divide and conquer. It can be implemented inplace so no extra memory is needed. For example, suppose 'S' is an array. . Divide If S has only 1 element, return the array without any sorting, otherwise choose an element, x, in S. This element is called the pivot . Divide S into 3 sets, L, E, and G, such that all elements in L are less than x, all elements in G are greater than x, and all elements in E=x. Recursively call Quicksort Quicksort(L) Quicksort(G) Conquer Put the elements back into S Example of Quicksort: *Need some array, 'S', for storage of the elements. *To separate S into L, E, and G, we will need a left and right index that will point to the beginning and end of S. *The pivot is placed in its final position after the separation of L, E, and G. Code for a quicksort in C++:

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

View Full Document
Complexity of this algorithm Worst case occurs when the pivot is chosen such that L or G are empty Worst Case-> O(n^2) Best Case-> O(nlogn) Raddick's Sort Used for sequences of elements that are represented with a maximum number of bits.
*Represents numbers in binary. *For all positions of the bits, examine all of the elements. *Rearrange the elements such that the elements with 0's come first and the elements with 1's come after. Code for Raddick's sort in C++:

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

View Full Document
Bucket Sort Sort n numbers in the range (1,2,3,. .,n). Only catch: Limited up to m numbers. First, create an array of m entries, called buckets , that will store counters of the number of elements for each value. Do not forget to initalize these counters to 0. For example: (1,5,4,3,3,2,1,5) m = 5 (the values are from 1 to 5) n = 8 (the number of total values) Left to right, count the number of occurances of each number in the array. Scan the buckets in order and add in the sequence as many values as the counters
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

### Page1 / 11

notes61 - Quick Sort Popular sorting algorithm that uses...

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

View Full Document
Ask a homework question - tutors are online