sorting algorithms in java
DESCRIPTION
Sorting Algorithms in Java. Complexity. Algorithms. Interfaces. Topics: Problem definition Insertion Sort Selection Sort Counting Sort Merge Sort Collections.sort Teaching material for the course of "Tecniche di Programmazione" at Politecnico di Torino in year 2012/2013. More information: http://bit.ly/tecn-progrTRANSCRIPT
![Page 1: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/1.jpg)
Sorting
Tecniche di Programmazione – A.A. 2012/2013
![Page 2: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/2.jpg)
Summary
1. Problem definition
2. Insertion Sort
3. Selection Sort
4. Counting Sort
5. Merge Sort
6. Quicksort
7. Collections.sort
A.A. 2012/2013 Tecniche di programmazione 2
![Page 3: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/3.jpg)
Summary
1. Problem definition
2. Insertion Sort
3. Selection Sort
4. Counting Sort
5. Merge Sort
6. Quicksort
7. Collections.sort
A.A. 2012/2013 Tecniche di programmazione 3
Iterative
Recursive
Special
![Page 4: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/4.jpg)
Problem definition
Sorting
![Page 5: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/5.jpg)
Formal problem definition: Sorting
Input:
A sequence of n numbers <a1, a2, …, an>
Output:
A permutation <a’1, a’2, …, a’n> of the original elements, such
that a’1 a’2 … a’n
A.A. 2012/2013 Tecniche di programmazione 5
![Page 6: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/6.jpg)
Types of sorting approaches
Internal sorting
Data to be sorted are all within the main computer memory
(RAM)
Direct access to all element values
External sorting
Data to be sorted may not all be loaded in memory at the
same time
We must work directly on data stored on file
Typically, sequential access to data
A.A. 2012/2013 Tecniche di programmazione 6
![Page 7: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/7.jpg)
Sorting objects
A.A. 2012/2013 Tecniche di programmazione 7
Book-algorithms always refer to sorting sequences of
numbers
In practice, we need to sort the elements of a collection,
of some class type
The objects to be sorted must implement the
Comparable interface
![Page 8: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/8.jpg)
Comparable
A.A. 2012/2013 Tecniche di programmazione 8
public interface Comparable<T> (java.lang)
Must implement:
int compareTo(T other)
Returns a negative integer, zero, or a positive integer as this
object is less than, equal to, or greater than the specified other
object.
It is strongly recommended, but not strictly required
that (x.compareTo(y)==0) == (x.equals(y))
http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
![Page 9: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/9.jpg)
Sorting Comparable objects
A.A. 2012/2013 Tecniche di programmazione 9
Given a class, usually
A sub-set of the fields is used for sorting
The fields for sorting are called the «key» of the objects
.equals and .compareTo are defined according to the key fields
Other fields are regarded as «additional data»
Different types of keys (and thus ordering criteria) may be defined
The Comparable interface specifies the «natural» ordering
Other orderings may be achieved with the Comparator helper classes
![Page 10: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/10.jpg)
Comparator
A.A. 2012/2013 Tecniche di programmazione 10
public interface Comparator<T> (java.util)
Must implement:
int compare(T obj1, T obj2)
Returns a negative integer, zero, or a positive integer as the
first argument is less than, equal to, or greater than the second.
It is generally the case, but not strictly required
that (compare(x, y)==0) == (x.equals(y))
Comparators can be passed to a sort method
http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
![Page 11: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/11.jpg)
Example
A.A. 2012/2013 Tecniche di programmazione 11
public class Studente implements Comparable<Studente> {
private int matricola ; private String cognome ; private String nome ; private int voto ; @Override public int compareTo(Studente other) { return this.matricola - other.matricola ; }
![Page 12: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/12.jpg)
Example
A.A. 2012/2013 Tecniche di programmazione 12
public class Studente implements Comparable<Studente> {
private int matricola ; private String cognome ; private String nome ; private int voto ; @Override public int compareTo(Studente other) { return this.matricola - other.matricola ; }
«Natural» ordering:
by Matricola field
![Page 13: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/13.jpg)
Example
A.A. 2012/2013 Tecniche di programmazione 13
// Since we define compareTo, we should also redefine equals and hashCode !!! @Override public boolean equals(Object other) { return this.matricola == ((Studente)other).matricola ; } @Override public int hashCode() { return ((Integer)this.matricola).hashCode(); } ... getters & setters ...
}
Based on the same
«key» fields
![Page 14: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/14.jpg)
Comparator for sorting by name
A.A. 2012/2013 Tecniche di programmazione 14
public class StudenteByName implements Comparator<Studente> {
@Override public int compare(Studente arg0, Studente arg1) {
int cmp = arg0.getCognome().compareTo(arg1.getCognome()) ; if( cmp!=0 ) return cmp ; else return arg0.getNome().compareTo(arg1.getNome()) ;
} }
Check names only if
surnames are equal.
![Page 15: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/15.jpg)
Comparator for sorting by voto
A.A. 2012/2013 Tecniche di programmazione 15
public class StudenteByVoto implements Comparator<Studente> { @Override public int compare(Studente o1, Studente o2) { return o1.getVoto()-o2.getVoto() ; }
}
Note: repeated values for the
Voto field are possible
![Page 16: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/16.jpg)
Stability
A sorting algorithm is said to be stable when, if multiple
elements share the same value of the key, in the sorted
sequence such elements appear in the same relative
order of the original sequence.
A.A. 2012/2013 Tecniche di programmazione 16
![Page 17: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/17.jpg)
Algorithms
Various sorting algorithms are known, with differing
complexity:
O(n2): simple, iterative
Insertion sort, Selection sort, Bubble sort, …
O(n): applicable in special cases, only
Counting sort, Radix sort, Bin (o Bucket) sort, …
O(n log n): more complex, recursive
Merge sort, Quicksort, Heapsort
A.A. 2012/2013 Tecniche di programmazione 17
![Page 18: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/18.jpg)
Insertion Sort
Sorting
![Page 19: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/19.jpg)
Insertion sort
2 3 6 12 16 21 8
Already ordered Not considered yet
v[j]
2 3 6 8 12 16 21
Move right by one cell all elements ‘i’ for which v[i]>v[j]
2 3 6 8 12 16 21 5
A.A. 2012/2013 Tecniche di programmazione 19
![Page 20: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/20.jpg)
Quick reference
A.A. 2012/2013 Tecniche di programmazione 20
![Page 21: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/21.jpg)
Running example
A.A. 2012/2013 Tecniche di programmazione 21
![Page 22: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/22.jpg)
Complexity
Number of comparisons
Cmin = n-1
Cavg = ¼(n2+n-2)
Cmax = ½(n2+n)-1
Number of data copies
Mmin = 2(n-1)
Mavg = ¼(n2+9n-10)
Mmax = ½(n2+3n-4)
C = O(n2), M = O(n2)
T(n) = O(n2) Best case: already sorted vector
Worst case: inversely sorted vector
T(n) is not (n2)
Tworst case (n) = (n2)
A.A. 2012/2013 Tecniche di programmazione 22
![Page 23: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/23.jpg)
Selection Sort
Sorting
![Page 24: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/24.jpg)
Selection Sort
At every iteration, find the minimum of the yet-unsorted
part of the vector
Swap the minimum with the current position in the
vector
2 3 6 12 16 21 34 81 25 28 41 27 60
Already ordered Not ordered
v[j]
2 3 6 12 16 21 25 81 34 28 41 27 60
Mimimum
A.A. 2012/2013 Tecniche di programmazione 24
![Page 25: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/25.jpg)
Complexity
A.A. 2012/2013 Tecniche di programmazione 25
The loops don’t depend on the data stored in the array:
complexity is independent from the contents of the
values to be sorted
Worst case performance : О(n2)
Best case performance: О(n2)
Average case performance: О(n2)
![Page 26: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/26.jpg)
Implementation
A.A. 2012/2013 Tecniche di programmazione 26
public void sort(T[] vector, Comparator<T> comp) {
for(int j=0; j<vector.length; j++) {
// find minimum int pos = j ; for(int i=j+1; i<vector.length; i++) {
if( comp.compare(vector[i], vector[pos])<0 ) pos = i ;
} // swap positions if( j!=pos ) {
T temp = vector[pos] ; vector[pos] = vector[j] ; vector[j] = temp ;
} }
}
![Page 27: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/27.jpg)
Counting Sort
Sorting
![Page 28: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/28.jpg)
Counting sort
Not applicable in general
Precondition (hypothesis for applicability):
The n elements to be sorted are integer numbers ranging from
1 to k, for some positive integer k
With this hypothesis, if k = O(n), then the algorithm has
complexity O(n), only!
A.A. 2012/2013 Tecniche di programmazione 28
![Page 29: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/29.jpg)
Basic idea
Find, for each element x to be sorted, how many
elements are less than x
This information allows us to directly deposit x into its
final destination position.
A.A. 2012/2013 Tecniche di programmazione 29
![Page 30: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/30.jpg)
A.A. 2012/2013 Tecniche di programmazione 30
Data structures
We need 3 vectors:
Starting vector : A[1..n]
Result vector : B[1..n]
Support vector : C[1..k]
Vector C keeps track of the number of elements in A that
have a certain value:
C[i] = how many elements in A have value i
The sum of the first i elements in C equals the number of
elements in A with value <= i.
![Page 31: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/31.jpg)
Pseudo-code
A.A. 2012/2013 Tecniche di programmazione 31
![Page 32: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/32.jpg)
Analysis
For each j, C[A[j]] is the number of elements <=A[j], and
also represents the final position of A[j] in B:
B[ C[A[j]] ] = A[j]
The corrective term C[A[j]] C[A[j]] – 1 handles the
presence of duplicate items
A.A. 2012/2013 Tecniche di programmazione 32
![Page 33: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/33.jpg)
Example (n=8, k=6)
3 6 4 1 3 4 1 4 A
2 0 2 3 0 1 C
2 2 4 7 7 8 C
4 B 2 2 4 6 7 8 C
1 4 B 1 2 4 6 7 8 C
A.A. 2012/2013 Tecniche di programmazione 33
![Page 34: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/34.jpg)
Example
4 B 2 2 4 6 7 8 C
1 4 B 1 2 4 6 7 8 C
1 4 4 B 1 2 4 5 7 8 C
1 3 4 4 B 1 2 3 5 7 8 C
j=8
3 6 4 1 3 4 1 4 A
j=7
1 1 3 4 4 B 0 2 3 5 7 8 C
j=6
j=5
j=4
1 1 3 4 4 4 B 0 2 3 4 7 8 C j=3
1 1 3 4 4 4 6 B 0 2 3 4 7 7 C j=2
1 1 3 3 4 4 4 6 B 0 2 2 4 7 7 C j=1
A.A. 2012/2013 Tecniche di programmazione 34
![Page 35: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/35.jpg)
Complexity
1-2: Initialization of C: O(k)
3-4: Computaion of C: O(n)
6-7: Running sum in C: O(k)
9-11: Copy back to B: O(n)
Total complexity is therefore: O(n+k).
The algorithm is useful with k=O(n), only…
In such a case, the overall complexity is O(n).
A.A. 2012/2013 Tecniche di programmazione 35
![Page 36: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/36.jpg)
Quick Reference
A.A. 2012/2013 Tecniche di programmazione 36
![Page 37: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/37.jpg)
Merge Sort
Sorting
![Page 38: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/38.jpg)
Merge Sort
A.A. 2012/2013 Tecniche di programmazione 38
The Merge Sort algorithm is a direct application of the
Divide et Impera approach
6 12 4 5 2 9 5 12
6 12 4 5 2 9 5 12
4 5 6 12 2 5 9 12
2 4 5 5 6 9 12 12
Divide
Solve
Combine
Solve
![Page 39: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/39.jpg)
Merge Sort: Divide
The vector is simply partitioned in two sub-vector,
according to a splitting point
The splitting point is usually chosen at the middle of the
vector
6 12 4 5 2 9 5 12
6 12 4 5 2 9 5 12
Divide
1 8
1 4 5 8
p r
p r q q+1
A.A. 2012/2013 Tecniche di programmazione 39
![Page 40: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/40.jpg)
Merge Sort: Termination
Recursion terminates when the sub-vector:
Has one element, only: p=r
Has no elements: p>r
p r
p r q q+1
A.A. 2012/2013 Tecniche di programmazione 40
![Page 41: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/41.jpg)
Merge Sort: Combine
The combining step implies merging two sorted sub-
vectors
Recursion guarantees that the sub-vectors are sorted
The merging approach compares the first element of each of
the two vectors, and copies the lowest one
The result of the merging is saved in a different vector
Such algorithm may be realized in (n).
4 5 6 12 2 5 9 12
2 4 5 5 6 9 12 12
Combine
A.A. 2012/2013 Tecniche di programmazione 41
![Page 42: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/42.jpg)
Pseudo-code
Combine
Solve
Divide
Termination
MERGE-SORT(A, p, r)
1 if p < r
2 then q (p+r)/2
3 MERGE-SORT(A, p, q)
4 MERGE-SORT(A, q+1, r)
5 MERGE(A, p, q, r)
A.A. 2012/2013 Tecniche di programmazione 42
![Page 43: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/43.jpg)
Note
We often use the following symbols:
x = integer part of x, i.e. largest integer preceding x (floor
function)
x = smallest integer following x (ceiling function)
Examples:
3 = 3 = 3
3.1 = 3; 3.1 = 4
A.A. 2012/2013 Tecniche di programmazione 43
![Page 44: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/44.jpg)
The Merge procedure
MERGE(A, p, q, r)
1 i p ; j q+1 ; k 1
2 while( i q and j r )
3 if( A[i] < A[j]) B[k] A[i] ; i i+1
4 else B[k] A[j] ; j j+1
5 k k+1
6 while( iq ) B[k]A[i] ; ii+1; kk+1
7 while( jr ) B[k]A[j] ; jj+1; kk+1
8 A[p..r] B[1..k-1]
Complexity: (n).
A.A. 2012/2013 Tecniche di programmazione 44
![Page 45: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/45.jpg)
The Merge procedure
MERGE(A, p, q, r)
1 i p ; j q+1 ; k 1
2 while( i q and j r )
3 if( A[i] < A[j]) B[k] A[i] ; i i+1
4 else B[k] A[j] ; j j+1
5 k k+1
6 while( iq ) B[k]A[i] ; ii+1; kk+1
7 while( jr ) B[k]A[j] ; jj+1; kk+1
8 A[p..r] B[1..k-1]
At each iteration, the smallest number
between the heads of the two vectors is
copied to B
The «tail» of one of the vectors is
emptied
A.A. 2012/2013 Tecniche di programmazione 45
![Page 46: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/46.jpg)
Complexity analysis
Termination: a simple test, (1)
Divide (2): find the mid-point of the vector, D(n)=(1)
Solve (3-4): solves 2 sub-problems of size n/2 each,
2T(n/2)
Combine (5): based on the Merge algorithm, C(n) = (n).
A.A. 2012/2013 Tecniche di programmazione 46
![Page 47: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/47.jpg)
Complexity analysis
Termination: a simple test, (1)
Divide (2): find the mid-point of the vector, D(n)=(1)
Solve (3-4): solves 2 sub-problems of size n/2 each,
2T(n/2)
Combine (5): based on the Merge algorithm, C(n) = (n).
One sub-problem has size
n/2 , the other n/2 .
This detail does not change the
complexity result.
A.A. 2012/2013 Tecniche di programmazione 47
![Page 48: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/48.jpg)
Complexity
T(n) =
(1) for n 1
2T(n/2) + (n) for n > 1
The solution (proof omitted…) is:
T(n) = (n log n)
A.A. 2012/2013 Tecniche di programmazione 48
![Page 49: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/49.jpg)
Intuitive understanding (n=16)
16
8 8
4 4 4 4
2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 x 16 = n
2 x 8 = n
4 x 4 = n
8 x 2 = n
16 x 1 = n
log
2 n
Total operations: n log2 n
Recursion levels: log2 n Operations per level: n
A.A. 2012/2013 Tecniche di programmazione 49
![Page 50: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/50.jpg)
Warning
Not all recursive implementations have (n log n)
complexity.
For example, if merge sort is used with asymmetric
partitioning (q=p+1), it degrades to an insertion sort,
yielding (n2).
A.A. 2012/2013 Tecniche di programmazione 50
![Page 51: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/51.jpg)
Quicksort
Sorting
![Page 52: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/52.jpg)
Collections.sort
Sorting
![Page 53: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/53.jpg)
Sorting, in practice, in Java
A.A. 2012/2013 Tecniche di programmazione 74
A programmer’s motto says:
Use the system sort
i.e., the sorting algorithm already provided by your libraries
In other words, don’t re-implement your own sorting functions
The Collections framework provides:
public class Collections
This class consists exclusively of static methods that operate
on or return collections
public static <T extends Comparable<? super T>> void sort(List<T> list)
public static <T> void sort(List<T> list, Comparator<? super T> c)
![Page 54: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/54.jpg)
Collections.sort(list)
A.A. 2012/2013 Tecniche di programmazione 75
Sorts the specified list into ascending order, according to
the natural ordering of its elements.
All elements in the list must implement
the Comparable interface.
Furthermore, all elements in the list must be mutually
comparable (that is, e1.compareTo(e2) must not throw
a ClassCastException for any elements e1 and e2 in the list).
This sort is guaranteed to be stable: equal elements will
not be reordered as a result of the sort.
The specified list must be modifiable, but need not be
resizable. http://docs.oracle.com/javase/7/docs/api/java/util/Coll
ections.html#sort(java.util.List)
![Page 55: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/55.jpg)
Implementation of Collections.sort
A.A. 2012/2013 Tecniche di programmazione 76
This implementation is a stable, adaptive, iterative
mergesort that requires far fewer than n lg(n)
comparisons when the input array is partially sorted,
while offering the performance of a traditional mergesort
when the input array is randomly ordered.
If the input array is nearly sorted, the implementation
requires approximately n comparisons.
Temporary storage requirements vary from a small
constant for nearly sorted input arrays to n/2 object
references for randomly ordered input arrays.
http://docs.oracle.com/javase/7/docs/api/java/util/C
ollections.html#sort(java.util.List)
![Page 56: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/56.jpg)
Resources
A.A. 2012/2013 Tecniche di programmazione 77
Algorithms in a Nutshell, By George T. Heineman, Gary
Pollice, Stanley Selkow, O'Reilly Media
http://docs.oracle.com/javase/7/docs/api/java/lang/Compar
able.html
http://www.sorting-algorithms.com/
![Page 57: Sorting Algorithms in Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4458b4c90582328b5458/html5/thumbnails/57.jpg)
Licenza d’uso
A.A. 2012/2013 Tecniche di programmazione 78
Queste diapositive sono distribuite con licenza Creative Commons “Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)”
Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,
rappresentare, eseguire e recitare quest'opera
di modificare quest'opera
Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dell'opera agli autori
originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.
Non commerciale — Non puoi usare quest'opera per fini commerciali.
Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.
http://creativecommons.org/licenses/by-nc-sa/3.0/