06 Sorting
06 Sorting
Sorting
Acknowledgement
■ The contents of these slides have origin from
School of Computing, National University of
Singapore.
■ We greatly appreciate support from Mr. Aaron
Tan Tuck Choy, and Dr. Low Kok Lim for
kindly sharing these materials.
2
Policies for students
■ These contents are only used for students
PERSONALLY.
■ Students are NOT allowed to modify or
deliver these contents to anywhere or anyone
for any purpose.
3
Recording of modifications
■ Course website address is changed to
http://sakai.it.tdt.edu.vn
■ Course codes cs1010, cs1020, cs2010 are
placed by 501042, 501043, 502043
respectively.
4
Objectives
29 10 14 13 37
13 10 14 29 37
13 10 14 29 37
10 13 14 29 37 Sorted!
■ … a[j] = a[j+1];
■ When i=(n-1), 1 iteration
a[j+1] = temp;
■ Total number of iterations = (n-1) + (n-2) + …} + 1 }
= n×(n-1)/2 }
}
■ Worst case
❑ Input in descending order
❑ How many iterations in the outer loop are needed?
Answer: n-1 iterations
❑ Running time remains the same: O(n2)
■ Best case
❑ Input is already in ascending order
❑ The algorithm returns after a single iteration in the
outer loop. (Why?)
❑ Running time: O(n)
■ n=4 S1 S2
■ Given a seq: 40 13 20 8
■ i=1 13 40 20 8
■ i=2 13 20 40 8
■ i=3 8 13 20 40
■ n = no of items to be sorted
■ S1 = sub-array sorted so far
■ S2 = elements yet to be processed
■ In each iteration, how to insert the next element into
S1 efficiently?
// Now insert the value next after index j at the end of loop
a[j+1] = next;
}
}
InsertionSort.java
◈
[501043 Lecture 12: Sorting]
27
3 Analysis of Insertion Sort
■ Outer loop executes exactly n-1 times
■ Number of times inner loop executes depends on the
inputs:
❑ Best case: array already sorted, hence (a[j] > next) is always false
■ No shifting of data is necessary; Inner loop not executed at all.
❑ Worst case: array reversely sorted, hence (a[j] > next) is always
true
■ Need i shifts for i = 1 to n-1.
■ Insertion always occurs at the front.
... insertionSort(int[] a) {
■ Therefore, the best case for (int i=1;i<a.length;i++) {
running time is O(n). int next = a[i];
int j;
(Why?) for (j=i-1; j>=0 && a[j]>next;
j--)
■ The worst case running a[j+1] = a[j];
7 2 6 3 8 4 5
Divide into
two halves 7 2 6 3 8 4 5
Recursively
sort the halves 2 3 6 7 4 5 8
38 16 39 12
16 38 12 39
16 27 38 12 27 39
12 16 27 27 38 39
[501043 Lecture 12: Sorting]
35
4 Merge Sort of a 6-element Array (2/2)
mergeSort(a,i,mid);
mergeSort(a,mid+1,j);
merge(a,i,mid,j);
38 16 27 39 12 27
38 16 27 39 12 27 Divide phase:
Recursive call to
38 16 27 39 12 27 mergeSort
38 16 39 12
16 38 12 39
Conquer phase:
16 27 38 12 27 39 Merge steps
The sorting is done
here
12 16 27 27 38 39
[501043 Lecture 12: Sorting]
36
4 How to Merge 2 Sorted Subarrays?
Temp array
t[0..5] a[0..2] a[3..5]
245 378
2 245 378
2 3 245 378
2 3 4 245 378
2 3 4 5 2 45 378
2 3 4 5 7 8 245 378
Level 1: Level 1:
2 calls to Mergesort n/2 items n/2 n/2 1 calls to Merge
Level 2: Level 2:
4 calls to Mergesort n/22 items n/22 n/22 n/22 n/22 2 calls to Merge
…
Level (log n):
Level (log n): 2(log n) -1(= n/2)
n calls to Mergesort 1 item 1 1 …………………… 1 1 calls to Merge
Pivot
Recursively sort
the two parts 12 16 27 27 38 39
p <p ≥p ?
i m k j
S1 S2 Unknow
[501043 Lecture 12: Sorting] n 48
5 Partition algorithm idea (2/4)
■ Initially, regions S1 and S2 are empty. All items
excluding p are in the unknown region.
■ Then, for each item a[k] (for k=i+1 to j) in the unknown
region, compare a[k] with p:
❑ If a[k] ≥ p, put a[k] into S2.
❑ Otherwise, put a[k] into S1.
■ Q: How about if we change ≥ to > in the condition
part?
S1 S2
If a[k] =y ≥ p, p <p x ≥p y ?
i m k j
S1 S2
Increment k p <p x ≥p y ?
i m k j
Increment m p <p x ≥p y ?
i m k j
p <p y ≥p x ?
Swap x and y
i m k j
Increment k p <p y ≥p x ?
i m k j
[501043 Lecture 12: Sorting]
51
5 Code of Partition Algorithm
... partition(int[] a, int i, int j) {
// partition data items in a[i..j]
int p = a[i]; // p is the pivot, the ith item
int m = i; // Initially S1 and S2 are empty
for (int k=i+1; k<=j; k++) { //process unknown region
if (a[k] < p) { // case 2: put a[k] to S1
m++;
swap(a,k,m);
} else { // case 1: put a[k] to S2. Do nothing!
} // else part should be removed since it is empty
}
swap(a,i,m); // put the pivot at the right place
return m; // m is the pivot’s final position
}
■ As there is only one ‘for’ loop and the size of the array is
n = j – i + 1, so the complexity for partition() is O(n)
[501043 Lecture 12: Sorting]
52
5 Partition Algorithm: Example
10 13 14 29 37
p S2
S1 is empty
1 n-1
Quick sort:
1285 5 150 4746 602 5 8356 // pivot in bold
1285 (5 150 602 5) (4746 8356)
5 5 150 602 1285 4746 8356 //pivot swapped with the last one in S1
// the 2 5’s are in different order of the initial list
Selection sort: select the largest element and swap with the last one
1285 5 4746 602 5 (8356)
1285 5 5 602 (4746 8356)
602 5 5 (1285 4746 8356)
5 5 (602 1285 4746 8356)
// the 2 5’s are in different order of the initial list
[501043 Lecture 12: Sorting]
64
7 Summary of Sorting Algorithms
Worst Best Case In-place? Stable?
Case
Selection Sort O(n2) O(n2) Yes No
Sort2.java
◈
[501043 Lecture 12: Sorting]
70
8 Example: class Person
class Person {
private String name;
private int age;
Note: compare() and equals() are two methods of the interface Comparator.
Need to implement them.
[501043 Lecture 12: Sorting]
72
8 Comparator: NameComparator
import java.util.Comparator;
class NameComparator implements Comparator<Person> {
} // end TestComparator
TestComparator.java
Sorting by age:
[Mimi – 9, Sarah – 12, Mark – 12, Michael – 15, Andrew – 15]
◈
[501043 Lecture 12: Sorting]
76
8 Another solution using Arrays.sort( )
We can replace the statements
List<Person> list = Arrays.asList(p);
System.out.println("Sorting by age:");
Collections.sort(list, ageComp);
System.out.println(list + "\n");
with
System.out.println("Sorting by age using Arrays.sort():");
Arrays.sort(p, ageComp);
System.out.println(Arrays.toString(p) + "\n");
■ http://visualgo.net 🡪 http://visualgo.net/sorting.html
■ http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.
html
■ http://max.cs.kzoo.edu/~abrady/java/sorting/
■ http://www.sorting-algorithms.com/
■ http://en.wikipedia.org/wiki/Sort_algorithm
■ http://search.msn.com/results.aspx?q=sort+algorithm&FORM
=SMCRT
■ and others (please google)