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

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

CSE 143 Lecture 16 (B) Sorting reading: 13.1, 13.3 - 13.4 slides created by Marty Stepp http://www.cs.washington.edu/143/

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

View Full Document
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 , …
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]

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

View Full Document
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
5 Sorting algorithms bogo sort : shuffle and pray

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 / 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
Ask a homework question - tutors are online