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

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

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

Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park

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

View Full Document
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)
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

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

View Full Document
Sorting Comparison sort Only uses pairwise key comparisons Proven lower bound of O( n log(n) ) Linear sort Uses additional properties of keys
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
7 8 2 5 4 { 7, 2, 8, 5, 4 }
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 31

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

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

View Full Document
Ask a homework question - tutors are online