27-sorting - CSE 143 Lecture 27 Sorting reading 13.1 13.3 13.4 slides created by Marty Stepp http/www.cs.washington.edu/143 Sorting sorting

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

View Full Document Right Arrow Icon
CSE 143 Lecture 27 Sorting reading: 13.1, 13.3 - 13.4 slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 – can be solved in many ways: • there are many sorting algorithms • some are faster/slower than others • some use more/less memory than others • some work better with specific kinds of data • some can utilize multiple computers / processors, . .. comparison-based sorting : determining order by comparing pairs of elements: •< , > , compareTo , …
Background image of page 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]
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
5 Sorting algorithms bogo sort : shuffle and pray bubble sort
Background image of page 5

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

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

This note was uploaded on 10/30/2011 for the course COMPUTER S 143 taught by Professor John during the Spring '11 term at Washington State Community College.

Page1 / 20

27-sorting - CSE 143 Lecture 27 Sorting reading 13.1 13.3 13.4 slides created by Marty Stepp http/www.cs.washington.edu/143 Sorting sorting

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

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