{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec15

# lec15 - CSE 8A Lecture 15 Sorting selection sort bubble...

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

Page 1 of 29 CSE 8A, UCSD LEC 15 CSE 8A: Lecture 15 Sorting: selection sort, bubble sort, insertion sort Sorting efficiency Java interfaces Generic sorting and the java.util.Comparator interface (Reading: Savitch, Ch. 6 and Ch. 13)

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

View Full Document
Page 2 of 29 CSE 8A, UCSD LEC 15 Sorting Sorting is the process of re-arranging the elements of a sequence so that they are in order, according to some ordering relation There are many algorithms for sorting; entire books have been written on them Sorting is useful in many contexts: it can make subsequent human or machine processing of data much more efficient ... consider how much harder it would be to find someone’s phone number if the phone book weren’t sorted! We will look at some ways to definite a sorting function with header // PRE: a is an array of doubles // POST: the elements of a are in increasing order: // a[0] <= a[1] && a[1] <= a[2] &&...&& a[len-2] <= a[a.length-1] void sort(double a[]) This function can only sort arrays of doubles, into ascending order... we will consider later how to make it more generic
Page 3 of 29 CSE 8A, UCSD LEC 15 Selection sort Suppose you have an array of length N to sort. You could proceed this way: Search the entire array to select the smallest element in the array. Once found, swap it with the first element of the array. Now the first element of the array is correct; but we need to sort the remainder of the array Search all but the first element of the array to select the second smallest element in the array. Once found, swap it with the second element of the array. Now the first two elements of the array are correct; but we need to sort the remainder of the array Search all but the first two elements of the array to select the third smallest element in the the array. Once found, swap it with the third element of the array. Now the first three elements of the array are correct; but we need to sort the remainder of the array ... etc, until the first N elements (i.e. the entire array) are sorted. This is the idea behind the selection sort algorithm. In pseudocode: for (int n=0; n < a.length; n++) { Select the nth smallest element of a; Let m be its index; Swap a[m] with a[n] }

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

View Full Document
Page 4 of 29 CSE 8A, UCSD LEC 15 Selection sort: implementation // PRE: a is an array of len doubles // POST: the elements of a are in increasing order: // a[0] <= a[1] && a[1] <= a[2] && ... && a[len-2] <= a[len-1] void sort(double a[]) { int len = a.length, m; for (int n=0; n<len; n++) { // find the index of the smallest element in a[n],...,a[len-1] // this is the nth smallest element in the whole array m = selectSmallest(a,n); //... and swap it with a[n] swap(a,n,m); // loop invariant: the elements a[0],...,a[n] are sorted } }
Page 5 of 29 CSE 8A, UCSD LEC 15 Finding the smallest element’s index in a part of the array A swap() method is easy to define. Here is a selectSmallest method: // PRE: a is an array of doubles // 0 <= n < a.length // RETURNS: the index of the smallest element among // a[n], a[n+1], ... , a[a.length-1] int selectSmallest(double a[], int n) { double small = a[n]; // first candidate for smallest value int smallIndx = n; // first candidate for index of smallest for (int m=n+1; n<a.length; m++) {

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 ]}

### Page1 / 29

lec15 - CSE 8A Lecture 15 Sorting selection sort bubble...

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

View Full Document
Ask a homework question - tutors are online