Sorting
Chapter 18
18
Creating Sortable Objects
Objects are sortable when they
implement the Comparable interface.
Must implement compareTo()...
18
Sorting Arrays
TreeSet collections are sorted by
default.
Arrays class includes a sort() method.
Items in array must be...
18
Sorting Algorithms
Sortable interface
Provides framework for sort classes
Defines what it means to be sortable
SortList...
18
The Nested SortItem Class
SortItem class:
Implements Comparable interface
Uses the compareTo() method to
compare curren...
18
Sorting Algorithms
SortDriver class:
Creates a sort object and submits a list to test
the algorithm
Will be modified se...
18
Insertion Sort
Recursive insertion:
Compare new object
to each object in the
list until you find
one of lesser value.
R...
18
Shell Sort
The shell sort is a variation on the
insertion sort.
Divides list into smaller sublists
Sorts each sublist i...
18
Selection Sort
Selection sort:
Scans list to find lowest value then moves
it to the left
Repeat to find next lowest val...
18
Bubble Sort
Bubble sort:
Compares each item to its neighbor and
exchanges if necessary
Largest items “bubble” to the to...
18
Quicksort
Most widely used sort method
Partitions the list
Use “pivot” to create right and left side
Move item from lef...
18
Comparison of Sort Algorithms
Direct comparison
using random values
Upcoming SlideShare
Loading in …5
×

Chapter 18

697 views

Published on

Published in: Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
697
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 18

  1. 1. Sorting Chapter 18
  2. 2. 18 Creating Sortable Objects Objects are sortable when they implement the Comparable interface. Must implement compareTo() method Programmer must decide how to compare objects.
  3. 3. 18 Sorting Arrays TreeSet collections are sorted by default. Arrays class includes a sort() method. Items in array must be sortable (implement the Comparable interface).
  4. 4. 18 Sorting Algorithms Sortable interface Provides framework for sort classes Defines what it means to be sortable SortList class Holds the items in the list in an internal ArrayList Tracks: Number of items in the list Number of comparisons Number of exchanges Indicates whether algorithm steps should be displayed
  5. 5. 18 The Nested SortItem Class SortItem class: Implements Comparable interface Uses the compareTo() method to compare current object to an object passed in Uses the exchangeValues() method to exchange values when required
  6. 6. 18 Sorting Algorithms SortDriver class: Creates a sort object and submits a list to test the algorithm Will be modified several times as new sort algorithm classes are created SortAlgorithm class: Abstract base class (used to derive all sort algorithm classes) Provides common methods used by all the sort algorithms
  7. 7. 18 Insertion Sort Recursive insertion: Compare new object to each object in the list until you find one of lesser value. Requires a lot of comparisons and exchanges.
  8. 8. 18 Shell Sort The shell sort is a variation on the insertion sort. Divides list into smaller sublists Sorts each sublist individually Then sorts sublists together Requires many comparisons, but fewer exchanges
  9. 9. 18 Selection Sort Selection sort: Scans list to find lowest value then moves it to the left Repeat to find next lowest value Many comparisons with minimal exchanges
  10. 10. 18 Bubble Sort Bubble sort: Compares each item to its neighbor and exchanges if necessary Largest items “bubble” to the top Poor performance due to many comparisons and many exchanges
  11. 11. 18 Quicksort Most widely used sort method Partitions the list Use “pivot” to create right and left side Move item from left to right if larger than pivot Move item from right to left if smaller than pivot Each side is then partitioned and algorithm recurses until list is sorted
  12. 12. 18 Comparison of Sort Algorithms Direct comparison using random values

×