(19) Sorting

(19) Sorting - Reading CMSC 132: Object-Oriented...

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

View Full Document Right Arrow Icon
CMSC 132: Object-Oriented Programming II Sorting 1 Department of Computer Science University of Maryland, College Park Reading Read Chapter 10 in the Koffman text (Chapter 8 in the second edition) Sorting Sorting refers to rearranging a list of elements in a predetermined order, based on the key for each element Derived from ability to compare two keys by size Properties that sorting algorithms can have: table eans the relative order of equal keys 3 Stable means the relative order of equal keys remains 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 An in-place sorting algorithm uses only a constant amount of additional space An external sort sorts data that's not in memory (on disk), and has to be able to efficiently sort a large number of keys Types of sorting algorithms A comparison sort only uses pairwise key comparisons Comparison sorting has a proven lower bound of O (n log(n)) A linear sort is more efficient, but relies on 4 some additional properties of keys
Background image of page 1

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

View Full DocumentRight Arrow Icon
Bubble sort Approach 1. Repeatedly sweep through shrinking portions of the list of elements to be sorted 2. Swap an element x with its right neighbor if x is larger 5 Bubble sort example 7 2 8 5 4 2 7 8 5 4 2 7 5 4 8 2 7 5 4 8 2 5 4 7 8 2 5 4 7 8 2 4 5 7 8 2 4 5 7 8 pass 1 pass 2 pass 3 pass 4 6 2 7 8 5 4 2 7 5 8 4 2 7 5 4 8 2 5 7 4 8 2 5 4 7 8 2 4 5 7 8 Bubble sort code void bubbleSort(int[] a) { int outer, inner; for (outer= a.length - 1; outer > 0; outer--) for (inner= 0; inner < outer; inner++) if (a[inner] > a[inner + 1]) swap(a, inner, inner + 1); } swap with ght neighbor sweep through array 7 void swap(int a[], int x, int y) { int temp= a[x]; a[x]= a[y]; a[y]= temp; } right neighbor if larger swap array elements at positions x and y Performance O (n 2 ) in the average and worst case Selection sort Approach 1. Iteratively sweep through shrinking portions of list 2. Select smallest element found in each sweep 7 2 8 5 4 2 7 8 5 4 Example: 8 3. Swap smallest element with front of current list 2 4 8 5 7 2 4 5 8 7 2 4 5 7 8
Background image of page 2
Selection sort code void selectionSort(int[] a) { int outer, inner, min; for (outer= 0; outer < a.length - 1; outer++) { min= outer; for (inner= outer + 1; inner < a.length; nner++) sweep through array 9 inner++) if (a[inner] < a[min]) min= inner; swap(a, outer, min); } } swap with smallest element found find smallest element Performance O (n 2 ) in the average and worst case Tree sort Approach 1. Insert elements in a binary
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/16/2011 for the course CMSC 132 taught by Professor Padua-perez during the Fall '08 term at Maryland.

Page1 / 11

(19) Sorting - Reading CMSC 132: Object-Oriented...

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

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