# 14-sort - CSE 143 Lecture 14 Sorting slides created by...

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

1 CSE 143 Lecture 14 Sorting slides created by Marty Stepp and Ethan Apter http://www.cs.washington.edu/143/ 2 Sorting sorting : Rearranging the values in an array or collection into a specific order (usually into their "natural ordering"). – one of the fundamental problems in computer science • bogo sort • bubble sort • selection sort • insertion sort • merge sort • heap sort • quick sort • bucket sort • radix sort • ... • How would you sort a million integers?

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

View Full Document
2 3 Sorting methods in Java • The Arrays and Collections classes in java.util have a static method sort that sorts the elements of an array/list String[] words = {"foo", "bar", "baz", "ball"}; Arrays.sort(words); System.out.println(Arrays.toString(words)); // [ball, bar, baz, foo] List<String> words2 = new ArrayList<String>(); for (String word : words) { words2.add(word); } Collections.sort(words2); System.out.println(words2); // [ball, bar, baz, foo] 4 Collections class Method name Description binarySearch( list , value ) returns the index of the given value in a sorted list (< 0 if not found) copy( listTo , listFrom ) copies listFrom 's elements to listTo emptyList() , emptyMap() , emptySet() returns a read-only collection of the given type that has no elements fill( list , value ) sets every element in the list to have the given value max( collection ) , min ( collection ) returns largest/smallest element replaceAll( list , old , new ) replaces an element value with another reverse( list ) reverses the order of a list's elements shuffle( list ) arranges elements into a random order sort( list ) arranges elements into ascending order
3 5 Bogo sort bogo sort : Orders a list of values by repetitively shuffling them and checking if they are sorted. – name comes from the word "bogus" The algorithm: – Scan the list, seeing if it is sorted. If so, stop. – Else, shuffle the values in the list and repeat. 6 Bogo sort code // Places the elements of a into sorted order. public static void bogoSort(int[] a) { while (!isSorted(a)) { shuffle(a); } } // Returns true if a's elements are in sorted order. private static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; }

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

View Full Document
4 7 Bogo sort code, cont'd. // Shuffles an array of ints by randomly swapping each // element with an element ahead of it in the array. private static void shuffle(int[] a) { for (int i = 0; i < a.length - 1; i++) { // pick a random index in [i+1, a.length-1] int range = a.length - 1 - (i + 1) + 1; int j = (int) (Math.random() * range + (i + 1)); swap(a, i, j); } } // Swaps a[i] with a[j]. private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } 8 Selection sort selection sort : Orders a list of values by repeatedly putting the smallest unplaced value into its final position. The algorithm:
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/29/2010 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

### Page1 / 16

14-sort - CSE 143 Lecture 14 Sorting slides created by...

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

View Full Document
Ask a homework question - tutors are online