Sorting Algorithms by Nelson Padua-Perez & BIll Pugh

Sorting Algorithms by Nelson Padua-Perez & BIll...

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

View Full Document Right Arrow Icon
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park
Background image of page 1

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

View Full DocumentRight Arrow Icon
Overview Comparison sort Bubble sort Selection sort Tree sort Heap sort Quick sort Merge sort Linear sort Counting sort Bucket (bin) sort Radix sort O(n 2 ) O(n log(n) ) O(n)
Background image of page 2
Sorting Goal Arrange elements in predetermined order Based on key for each element Derived from ability to compare two keys by size Properties Stable relative order of equal keys unchanged Stable: 3 , 1, 4, 3 , 3 , 2 1, 2, 3 , 3 , 3 , 4 Unstable: 3 , 1, 4, 3 , 3 , 2 1, 2, 3 , 3 , 3 , 4 In-place uses only constant additional space External can efficiently sort large # of keys
Background image of page 3

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

View Full DocumentRight Arrow Icon
Sorting Comparison sort Only uses pairwise key comparisons Proven lower bound of O( n log(n) ) Linear sort Uses additional properties of keys
Background image of page 4
Bubble Sort Approach Iteratively sweep through shrinking portions of list Swap element x with its right neighbor if x is larger Performance O( n 2 ) average / worst case
Background image of page 5

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

View Full DocumentRight Arrow Icon
Bubble Sort Example 7 2 8 5 4 2 7 8 5 4 2 7 8 5 4 2 7 5 8 4 2 7 5 4 8 2 7 5 4 8 2 5 7 4 8 2 5 4 7 8 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 5 4 7 8 2 4 5 7 8 2 4 5 7 8 Sweep 1 Sweep 2 Sweep 3 Sweep 4
Background image of page 6
Bubble Sort Code void bubbleSort(int[] a) { for (int outer = a.length - 1; outer > 0; outer--) for (int inner = 0; inner < outer; inner++) if (a[inner] > a[inner + 1]) swap(a, inner, inner+1); } Sweep through array Swap with right neighbor if larger
Background image of page 7

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

View Full DocumentRight Arrow Icon
7 2 8 5 4 7 2 4 5 8 5 2 4 7 8 4 2 5 7 8 2 4 5 7 8 Example Selection Sort Approach Iteratively sweep through shrinking portions of list Select largest element found in each sweep Swap largest element with end of current list Performance O( n2 ) average / worst case
Background image of page 8
Selection Sort Code void selectionSort(int[] a) { for (int outer = a.length-1; outer > 0; outer--) { int max = 0; for (int inner = 1; inner <= outer; inner++) if (a[inner] > a[max]) max = inner; swap(a,outer,max); } } Swap with largest element found Sweep through array Find largest element
Background image of page 9

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

View Full DocumentRight Arrow Icon
7 8 2 5 4 { 7, 2, 8, 5, 4 }
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 31

Sorting Algorithms by Nelson Padua-Perez &amp;amp; BIll...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online