16b-sort - CSE 143 Lecture 16(B Sorting reading 13.1 13.3...

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

View Full Document Right Arrow Icon
CSE 143 Lecture 16 (B) 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 Document Right 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 Document Right Arrow Icon
4 Collections class arranges elements into a random order shuffle( list ) replaces an element value with another replaceAll( list , old , new ) reverses the order of a list's elements reverse( list ) returns largest/smallest element max( collection ) , min( collection ) returns a read-only collection of the given type that has no elements emptyList() , emptyMap() , emptySet() copies listFrom 's elements to listTo copy( listTo , listFrom ) Method name Description binarySearch( list , value ) returns the index of the given value in a sorted list (< 0 if not found) fill( list , value ) sets every element in the list to have the given value sort( list ) arranges elements into ascending order
Background image of page 4
5 Sorting algorithms bogo sort : shuffle and pray
Background image of page 5

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

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

{[ snackBarMessage ]}

Page1 / 20

16b-sort - CSE 143 Lecture 16(B Sorting reading 13.1 13.3...

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