Sorting AlgorithmsCSC212—Fall 2014 Dominique Thiebaut
Sorting
• The action of organizing a collection of comparable objects in a predefined order.
Insertion Sort
static public void InsertionSort( int[] A ) {for ( int i=1; i< A.length; i++ ) {
int temp = A[i];int j;for ( j=i-1; j>=0 && temp < A[j] ; j-- )
A[j+1] = A[j];A[j+1] = temp;
}}
Complexity
INSERTION SORT Expression O( ? )
Best Time Complexity
Average Time Complexity
Worst Time Complexity
In what cases will InsertionSort
be advantageous?
Selection Sort
static public void SelectionSort(int[] A) {for (int i = 0; i < A.length - 1; i++) {
int index = i;for (int j = i + 1; j < A.length; j++)
if (A[j] < A[index])index = j;
int smallerNumber = A[index];A[index] = A[i];A[i] = smallerNumber;
}}
Complexity
SELECTION SORT Expression O( ? )
Best Time Complexity
Average Time Complexity
Worst Time Complexity
Quicksort
6 3 5 2 7 1 9
Quicksort Relies on Partitioning of the Array
6 3 5 2 7 1 9
PIVOT
Quicksort Relies on Partitioning of the Array
6 3 5 2 7 1 9
PIVOT
i j
Quicksort Relies on Partitioning of the Array
6 3 5 2 7 1 9
i j
Quicksort Relies on Partitioning of the Array
6 3 5 2 7 1 9
i j
Quicksort Relies on Partitioning of the Array
1 3 5 2 7 6 9
i j
Quicksort Relies on Partitioning of the Array
1 3 5 2 7 6 9
i j
Quicksort Relies on Partitioning of the Array
1 3 5 2 7 6 9
i j
Quicksort Relies on Partitioning of the Array
1 3 5 2 7 6 9
i j
Quicksort Relies on Partitioning of the Array
1 3 5 2 7 6 9
i j
Quicksort Relies on Partitioning of the Array
1 3 5 2 7 6 9
i j
Quicksort Relies on Partitioning of the Array
1 3 5 2 7 6 9
Quicksort Relies on Partitioning of the Array
Quicksort Relies on Partitioning of the Array
1 3 5 2 7 6 9
Recurse… Recurse…
Quicksort Relies on Partitioning
ARRAY time
Demo
private static void swap(int[] A, int i, int j) {int temp = A[i];A[i] = A[j];A[j] = temp;
}
private static int partition(int[] A, int low, int high) {int index = low; // ( low + high )/2;
swap( A, index, low );
int pivot = A[low];
int i = low - 1;int j = high + 1;
while (true) {i++;while (i < high && A[i] < pivot)
i++;j--;while (j > low && A[j] > pivot)
j--;
if (i < j)swap(A, i, j);
elsereturn j;
}}
public static void quicksort(int[] A, int low, int high) {if (low < high) {
int pivotIndex = partition(A, low, high);quicksort(A, low, pivotIndex );quicksort(A, pivotIndex + 1, high );
}}
Average, Best, Worst Cases:
Demos (Applet) (set N=20)
QUICKSORT O( ? )
Best Time Complexity
Average Time Complexity
Worst Time Complexity
Back to Quicksort Demos
This time with N=1,000,000 (on Beowulf2)
Quiz
• How can we speed Quicksort up?
• In the worst case conditions?
• In the average case?
Quiz #2
• Could we use the recently seen heap to sort?
• How?
• Sketch analgorithm
• Complexity?
20
15
9
10
15
2030
15
1510
9
510
XX
10
1 2 3 4 5 6 7 8 9
5
30
We stopped here last time…
(SortingAlgs.java on beowulf2)
Sorting Non-Comparable Items
How To Sort an Array of Objects
class Tuple {public int x;public int y;Tuple( int xx, int yy ) { x=xx; y=yy; }public String toString() { return "(" + x + ", " + y + ") "; }
}…static public void InsertionSort( Tuple[] A ) {
for (int i = 1; i < A.length; i++) {Tuple temp = A[i];int j;for (j = i - 1; j >= 0 && temp < A[j]; j--)
A[j + 1] = A[j];A[j + 1] = temp;
}}
How To Sort an Array of Objects
class Tuple {public int x;public int y;Tuple( int xx, int yy ) { x=xx; y=yy; }public String toString() { return "(" + x + ", " + y + ") "; }
}…static public void InsertionSort( Tuple[] A ) {
for (int i = 1; i < A.length; i++) {Tuple temp = A[i];int j;for (j = i - 1; j >= 0 && temp < A[j]; j--)
A[j + 1] = A[j];A[j + 1] = temp;
}} Compiler Error!
Solution: Make Tuple implement
Comparable
class Tuple implements Comparable {public int x;public int y;Tuple( int xx, int yy ) { x=xx; y=yy; }public String toString() { return "(" + x + ", " + y + ") "; }
@Overridepublic int compareTo(Object o) {
return x- ((Tuple) o).x;}
}
…
static public void InsertionSort(Tuple[] A) {for (int i = 1; i < A.length; i++) {
Tuple temp = A[i];int j;for (j = i - 1; j >= 0 && temp.compareTo( A[j] ) < 0; j--)
A[j + 1] = A[j];A[j + 1] = temp;
}
this.x<o.x —> <0this.x==o.x —> 0this.x>o.x —> >0
Comparable Objects Automatically
Work with All Java Libraries
private static void heapDemo() {PriorityQueue<Tuple> heap = new PriorityQueue<Tuple>();heap.add( new Tuple( 5, 10 ) );heap.add( new Tuple( 15, 10 ) );heap.add( new Tuple( 50, 100 ) );heap.add( new Tuple( 3, 30 ) );
while ( !heap.isEmpty() ) System.out.println( heap.poll() );
}
(3, 30) (5, 10) (15, 10) (50, 100)
Sorting Stuff in Java
int[] arrays double[] arrays
float[] arrays String[] arrays
import java.util.Arrays;
int[] A = new int[] { 10, 2, -1, 10, 20, 30, 15, 17, 19, 21 };displayI( "Unsorted A: ", A );arrays.sort( A );displayI( "Sorted A: ", A );
Unsorted A: 10 2 -1 10 20 30 15 17 19 21 Sorted A: -1 2 10 10 15 17 19 20 21 30
YourObject[] Arrays Doublet[] Arrays
class Doublet {public int x;
public int y;Doublet( int xx, int yy ) { x=xx; y=yy; }public String toString() { return "("+x+","+y+")"; }
}
• Assume we cannot change Doublet to make it extend the Comparable class
• How can we still use the java sorting algorithms?
YourObject[] Arrays Doublet[] Arrays
import java.util.Arrays;import java.util.Comparator;
class DoubletComparator implements Comparator {public int compare( Object ob1, Object ob2 ) {
if ( ob1 == ob2 )return 0;
if ( ob1 == null )return -1;
if ( ob2 == null )return 1;
return (int) (((Doublet) ob1).x - ((Doublet ) ob2).x);}
}
ob1<ob2 —> -1 ob1==ob2 —> 0ob1>ob2 —> +1
YourObject[] Arrays Doublet[] Arrays
Doublet[] D = new Doublet[] { new Doublet( 1, 2 ), new Doublet( 10, 3 ), new Doublet( 2, 4 )};displayT( "Unsorted D: ", D );Arrays.sort( D, new DoubletComparator() );displayT( "Sorted D: ", D );
Unsorted D: (1,2) (10,3) (2,4) Sorted D: (1,2) (2,4) (10,3)
YourObject[] Arrays Doublet[] Arrays
What about ArrayLists, Lists, or LinkedLists?
java.util.Collections!
ArrayListsimport java.util.ArrayList;import java.util.Collections;
ArrayList<Doublet> L = new ArrayList<Doublet>();L.add( new Doublet( 1, 2 ) );L.add( new Doublet( 10, 3 ) );L.add( new Doublet( 3, 4 ) );System.out.println( "Unsorted L: " + L );
Collections.sort( L, new DoubletComparator() );
System.out.println( "Sorted L: " + L );
Unsorted L: [(1,2), (10,3), (3,4)]Sorted L: [(1,2), (3,4), (10,3)]
Problem 1: 4 billion numbers read from a fast device. We need to extract the 1000 largest numbers. How can we find the top 1000 numbers very fast?
Problem 2: 4 billion numbers read from a fast device. We need to know the top 1000 largest numbers. We do not have enough memory to store all 4 billion. 1) How can we still proceed and 2) find the top 1000 numbers very fast?