(19) Sorting

# (19) Sorting - Reading CMSC 132 Object-Oriented Programming...

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

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: Stable means the relative order of equal keys 3 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

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

View Full Document
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 8 5 4 2 7 5 4 8 2 5 7 4 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 pass 1 pass 2 pass 3 pass 4 6 2 7 5 8 4 2 7 5 4 8 2 5 4 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 right neighbor sweep through array 7 void swap(int a[], int x, int y) { int temp= a[x]; a[x]= a[y]; a[y]= temp; } 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 3. Swap smallest element 7 2 8 5 4 2 7 8 5 4 Example: 8 with front of current list 2 4 8 5 7 2 4 5 8 7 2 4 5 7 8
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; inner++) sweep through array 9 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.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern