Algorithms Sorting
Algorithms Sorting
Outline
21.1 Introduction
21.2 Collections Overview
21.3 Class Arrays
21.4 Interface Collection and Class Collections
21.5 Lists
21.6 Algorithms
21.6.1 Algorithm sort
21.6.2 Algorithm shuffle
21.6.3 Algorithms reverse, fill, copy, max and min
21.6.4 Algorithm binarySearch
21.7 Sets
21.8 Maps
21.9 Synchronization Wrappers
21.10 Unmodifiable Wrappers
21.11 Abstract Implementations
21.12 (Optional) Discovering Design Patterns: Design Patterns Used in
Package java.util
• Collection
– Data structure (object) that can hold other objects
• Collections framework
– Interfaces that define operations for various collection types
– Belong to package java.util
• Collection
• Set
• List
• Map
• Class Arrays
– Provides static methods for manipulating arrays
– Provides “high-level” methods
• Method binarySearch for searching sorted arrays
• Method equals for comparing arrays
• Method fill for placing values into arrays
• Method sort for sorting arrays
• List
– Ordered Collection that can contain duplicate elements
– Sometimes called a sequence
– Implemented via interface List
• ArrayList
• LinkedList
• Vector
ArrayList:
java.awt.Color[r=255,g=0,b=255] red white blue java.awt.Color
[r=0,g=255,b=255]
list:
BLACK YELLOW GREEN BLUE VIOLET SILVER GOLD WHITE BROWN BLUE GRAY SILVER
• sort
– Sorts List elements
• Order is determined by natural order of elements’ type
• Relatively fast
• shuffle
– Randomly orders List elements
• binarySearch
– Locates Object in List
• Returns index of Object in List if Object exists
• Returns negative value if Object does not exist
Sorted ArrayList: black blue pink purple red tan white yellow
Searching for: black
Found at index 0
• Set
– Collection that contains unique elements
– HashSet
• Stores elements in hash table
– TreeSet
• Stores elements in tree
ArrayList: [red, white, blue, green, gray, orange, tan, white, cyan,
peach, gray, orange]
Nonduplicates are:
orange cyan green tan white blue peach red gray
set:
black green grey orange red tan white yellow
• Map
– Associates keys to values
– Cannot contain duplicate keys
• Called one-to-one mapping
• Unmodifiable wrappers
– Converting collections to unmodifiable collections
– Throw UnsorrtedOperationException if attempts
are made to modify the collection
• Abstract implementations
– Offer “bare bones” implementation of collection interfaces
• Programmers can “flesh out” customizable implementations
– AbstractCollection
– AbstractList
– AbstractMap
– AbstractSequentialList
– AbstractSet