notes61

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

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

View Full Document Right Arrow Icon
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++:
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
*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++:
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online